I Start a second activity when my first activity is paused.
FirstActivity.java
#Override
public void onPause(){
super.onPause();
startActivity(new Intent(this, SecondActivity.class));
}
When I press on the homescreen button, SecondActivity will start but with a delay. In this delay, there is enough time to open a new app (like messenger for instance). However, when I open a new app, SecondActivity will not start anymore (it won't even call the onCreate method of SecondActivity).
How can i still start SecondActivity even when I open a new app?
Override the onBackPressed() method and start new activity from there, instead of adding your code into onPause().
#Override
public void onBackPressed()
{
startActivity(new Intent(this, SecondActivity.class));
super.onBackPressed();
}
I think I have a solution, if you would like to launch the new activity I would go with onStop, instead of onPause.
Start the new activity with the flag of: singleInstance
<activity ..
android:launchMode= "singleInstance" />
Please use onStop() in the second activity and call finish() there. so you killing the 2nd activity instance, when leaving the app. You can restart it easily.
I would like to programmatically simulate my app being destroyed, for debugging. However for convenience, I would like this to happen by pressing a button on the app's GUI. At the moment, I have the following in a fragment:
public void onClick(View v) {
switch (v.getId()){
case R.id.exitButton:
Log.i(TAG, "Exit pressed");
finish();
System.exit(0);
}
}
The problem is, onDestroy() never gets called. In fact, nor does onPause() either. (Or at least LogCat doesn't show the logs I write in them.) What am I doing wrong? (If I press the home button, onPause() does get called, so I don't think it's a problem with my onPause() or onDestroy() method
Thanks
Thanks to MysticMagic's link, and the adding of finish(), The following finally worked:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
I have a problem concerning startActivity(intent) and onStop. The Android API suggests to save data in onStop, which is what I am doing here:
public void onStop(){
super.onStop();
if(tosave)
{
Editor editor = sp.edit();
editor.putInt(getString(R.string.index_of_text_color), text_color_index);
editor.putInt(getString(R.string.index_of_background_color), background_color_index);
editor.commit();
}
}
However, I would like to start the next activity once it is saved- so I need to use an intent and startActivity(intent).
public void click(View v){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
My question is, does startActivity(intent) with the two flags call the onStop() method as it is finishing the application, or do I need to call finish()? Is it allowed for me to call finish() after I start a new activity? Or, is it because the new activity will be at the top, Android automatically calls onStop as it is in the background now?
There are so many questions about how startActivity(intent) works with the app cycle that I do not understand. It will be helpful if someone points me to a link.
Thank you!
EDIT: the intent is not in onStop. It is in another method which corresponds to a button. As soon as the button is clicked, I will need to save the data-- go to onstop-- and then go to the next activity. How can I do that?
You should save state in onPause(), not onStop(), because under certain conditions onStop() will never be called. You are guaranteed that onPause() will be called.
onPause() will always be called on your activity when another activity is shown in front of it.
That should solve your problem.
I know this has been asked. I browsed and tried whatever I have found but for some reason, it is not working for me.
my code is as follow
runOnUiThread(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(LoginActivity.this, HomepageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
I can not use
android:noHistory="true"
as my activity in some circumstances has to stay on the stack.
So, i have the following behaviour. I log in into my app with the LoginActivity, then it goes to the HomepageActivity, but if I press the Back bottom, the LoginActivity pops back again, which I do not want.
Any idea how I could fix this.
This can be done by calling finish() right after startActivity().
finish() destroys the current Activity and therefore removes it from the Stack.
Call finish() right before startActivity().
You can override onBackPressed method of your activity to do what you want
When a user presses the back button on an intent, the application should quit. How can I ensure the application quits when the back button is pressed?
In my Home Activity I override the "onBackPressed" to:
#Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
so if the user is in the home activity and press back, he goes to the home screen.
I took the code from Going to home screen Programmatically
Immediately after you start a new activity, using startActivity, make sure you call finish() so that the current activity is not stacked behind the new one.
EDIT
With regards to your comment:
What you're suggesting is not particularly how the android app flow usually works, and how the users expect it to work. What you can do if you really want to, is to make sure that every startActivity leading up to that activity, is a startActivityForResult and has an onActivityResult listener that checks for an exit code, and bubbles that back. You can read more about that here. Basically, use setResult before finishing an activity, to set an exit code of your choice, and if your parent activity receives that exit code, you set it in that activity, and finish that one, etc...
A better user experience:
/**
* Back button listener.
* Will close the application if the back button pressed twice.
*/
#Override
public void onBackPressed()
{
if(backButtonCount >= 1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}
The app will only exit if there are no activities in the back stack. SO add this line in your manifest android:noHistory="true" to all the activities that you dont want to be back stacked.And then to close the app call the finish() in the OnBackPressed
<activity android:name=".activities.DemoActivity"
android:screenOrientation="portrait"
**android:noHistory="true"**
/>
Why wouldn't the user just hit the home button? Then they can exit your app from any of your activities, not just a specific one.
If you are worried about your application continuing to do something in the background. Make sure to stop it in the relevant onPause and onStop commands (which will get triggered when the user presses Home).
If your issue is that you want the next time the user clicks on your app for it to start back at the beginning, I recommend putting some kind of menu item or UI button on the screen that takes the user back to the starting activity of your app. Like the twitter bird in the official twitter app, etc.
Use onBackPressedmethod
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
This will solve your issue.
First of all, Android does not recommend you to do that within the back button, but rather using the lifecycle methods provided. The back button should not destroy the Activity.
Activities are being added to the stack, accessible from the Overview (square button since they introduced the Material design in 5.0) when the back button is pressed on the last remaining Activity from the UI stack. If the user wants to close down your app, they should swipe it off (close it) from the Overview menu.
Your app is responsible to stop any background tasks and jobs you don't want to run, on onPause(), onStop() and onDestroy() lifecycle methods. Please read more about the lifecycles and their proper implementation here: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
But to answer your question, you can do hacks to implement the exact behaviour you want, but as I said, it is not recommended:
#Override
public void onBackPressed() {
// make sure you have this outcommented
// super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
To exit from an Android app, just simply use.
in your Main Activity, or you can use Android manifest file to set
android:noHistory="true"
finish your current_activity using method finish() onBack method of your current_activity
and then add below lines in onDestroy of the current_activity for Removing Force close
#Override
public void onDestroy()
{
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
I modified #Vlad_Spays answer so that the back button acts normally unless it's the last item in the stack, then it prompts the user before exiting the app.
#Override
public void onBackPressed(){
if (isTaskRoot()){
if (backButtonCount >= 1){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Toast.makeText(this, "Press the back button once again to close the application.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}else{
super.onBackPressed();
}
}
you can simply use this
startActivity(new Intent(this, Splash.class));
moveTaskToBack(true);
The startActivity(new Intent(this, Splash.class)); is the first class that will be lauched when the application starts
moveTaskToBack(true); will minimize your application
Add this code in the activity from where you want to exit from the app on pressing back button:
#Override
public void onBackPressed() {
super.onBackPressed();
exitFromApp();
}
private void exitFromApp() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}