I always want to show password activity when my Android app is on pause.
For example, when the user goes to home screen or switches app using Recents screen, password activity should appear right away to hide contents and protect to resume app. However, startActivity is not working as expected inside onPause method.
override fun onPause() {
super.onPause()
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}
It actually executes startActivity when the App goes onPause. However, the onCreate of MainActivity2 is not being called right away, but called when the app resumes. It was same inside onStop by the way.
Any solution or idea for this?
Edit 1
AndroidManifest.xml
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.Droid"
tools:targetApi="31">
<activity
android:name=".MainActivity2"
android:exported="false"
android:label="#string/title_activity_main2"
android:theme="#style/Theme.Droid">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onPause() {
super.onPause()
val intent = Intent(this, MainActivity2::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}
}
MainActivity2.kt
class MainActivity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
}
These are the example code that I've tried, but still doesn't work. The process I've tried is like this. I wish that the onCreate of MainActivity2 is called right after the Home key is pressed.
Launch app → Home key → Recents screen (Doesn't show MainActivity2) → Resume app (onCreate of MainActivity2 is called)
Startin activty in onPause() is not recommended but even then if you want to start it, then use FLAG_ACTIVITY_NEW_TASK while starting. This tells system to start new activity in new instance so that when current activity gets is killed by system, the new activity has time to start since its in another instance.
Here is the code :
#Override
public void onPause() {
super.onPause();
Intent intent = new Intent(this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
This should help you.
Related
this the first part of the code with the splash screen code
public class MainActivity extends AppCompatActivity {
private static final int SPLASH_TIME_OUT = 1000;
private static final String FILE_NAME = "example.txt";
EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = findViewById(R.id.edit_text);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent homeIntent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
the first thing that comes out after i run the app is the main activity and not the splash screen
You have to specify your activity Actions by using Intent Filter ,
Intent Filters specifies the types of intents that an activity, service, or broadcast receiver can respond to. An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.
As in For your Case, You have to specify that MainActivity/SplashActivity should be on Launcher Mode(Launcher mode is an instruction for Android OS which specifies how the activity should be launched), you have to use Intent filter and specify Activity Actions ,
*Must go through this Article App Manifest Overview
Add this in your Manifest file within <Application> Tag
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In your AndroidManifest file, SplashActivity must be set as Launcher activity. Check the name of the Splash screen activity file in your project.
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Can you share your manifest? Launch Activity should be Splash Activity.
Which Activity you want the application to start with, you need to add IntentFilter in that Activity in the Manifest file.
<activity android:name="SplashActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
I have an application that has two main activities MainActivity1 and MainActivity2.
I have the Intent filter for the launcher on MainActivity1
activity android:name=".MainActivity1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
what I want is to be able to switch the launcher activity in my settings activity.
Is that possible and how will it work?
All I did was to create a shared preference boolean the know which activity to open. Then from MainActivity1 I used an intent to go to MainActivity2
Intent intent = getIntent();
boolean firstTimeOpening = intent.getBooleanExtra("BOOLEAN_FIRST_TIME_OPENING", true);
SharedPreferences preferences = getSharedPreferences("SHARED_PREFERENCES", MODE_PRIVATE);
boolean startAppWithActivity1 = preferences.getBoolean(START_APP_WITH_ACTIVITY1, true);
if(!startAppWithActivity1 && firstTimeOpening){ startActivity(new Intent(MainActivity1.this, MainActivity2.class));
}
Easy enough. The firstTimeOpening boolean makes sure the the switching
of activity occurs only when the application is just opened
please help , when i run the app, the app launch a Fatal Error im learning
just i create a instat with a call but not run, (sorry for my english)
.......................................................................
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.octa.appprueba3">
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.APP_CONTACTS" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
my code:
public class SecondActivity extends AppCompatActivity {
private EditText editPhoneText;
private ImageButton imageCallButton;
private final int PHONE_CALL_CODE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editPhoneText = (EditText) findViewById(R.id.editTextPhone);
imageCallButton = (ImageButton) findViewById(R.id.imageCallButton1);
imageCallButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
implicito();
}
});
}
public void implicito(){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("Tel: 9999999" ));
startActivity(intent);
}
}
You may want to check whether your device can handle your intent by doing this:
PackageManager packageManager = getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent);
} else {
Log.d(TAG, "Cannot handle this intent");
}
What's more, if you are placing a call like so, there is NO need to declare the permission in your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
Because you are not directly calling someone within your app. You are actually transferring the "duty" to other apps which can handle your intent to call. This doesn't need a permission.
To directly make a call within your app, the intent should be Intent.ACTION_CALL, which needs the permission you declared.
Hope this will help.
There is no activity on your device that handles ACTION_DIAL for a Tel: scheme. Try:
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:9999999" ));
Case and whitespace are important when assembling a Uri.
Also note that even my revised Intent will not work on all devices, such as on an Android tablet that lacks a dialer.
What I am attempting to do is to open "Items" when the ImageButton "ibItem1" is pressed. But, after setting up this:
ibItem1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent items = new Intent("android.intent.action.ITEMS");
startActivity(items);
}
});
And clicking the button does not do anything. I have the activity all set up in the manifest:
<activity
android:name="com.example.custombuilds.Items"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ITEMS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The program has crashed before because of a ActivityNotFoundException, even though I declared it in the manifest.
Intent myIntent = new Intent(this, NextActivity.class);
startActivity(myIntent);
In this form, it should work..
Also make sure that the name of the class that implements the activity is the same in the manifest file at the android:name="atributevalue".The attribute value should be a fully qualified class name.
I have 2 Activitys
MainActivity
LoginActivity
I want verify if user has logged, else show my LoginActivity before the MainActivity apears.
When I put this in OnCreateView of MainActivity I have an error:
if(Login.isLogged()){
selectItem(0);//starts the fragment
}
else {
Intent i = new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
}
FATAL EXCEPTION: MAIN java.lang.RuntimeException: Unable to start activity ComponentInfo { .....MainActivity}: java.lang.NullPointerException
Ps: My MainActivity is an SherlockFragmentActivity. I'm using DrawerLayout.
How can i start my LoginActivity before SherlockFragmentActivity?
Thanks
If I were you I would put LoginActiviy as launcher activity. There I would check if user has already logged in, if he has start new activity.. Something like this:
//in onCreate of LoginActivity
if (hasLogedIn) {
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
finish();
} else {
// show him login activity, setContentView etc...
// save hasLogin boolean as true
}
make this changes in your AndroidManifest file
<activity
android:name="com.example.alphabets.LoginActivity"
android:label="#string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.alphabets.MainActivity"
android:label="#string/app_name" >
</activity>
Make Login Activity as your default activity. Check Login status. If user is logged in, call Main Activity and finish() the Login Activity. else, wait for user input