In my Application, activity A is launcher activity, from A it called B and from B it called C, I have such more than 5 activities. In C when I press home button, and again o
on pressing home ,u have not finished activity it still exists in stack.only it goes to pause so use this.
if you are calling activity B from an activity A.and C from B
A->B
use startactivityforresult from A
and again B->C
use startactivityforresult from B
and when you want to exit from C then setResult(i.e. RESULT_OK) and finish C.and in OnActivityResult() in B,check if resultcode == RESULT_OK then again finish B and setresult(RESULT_OK) for A.same procedure will follow to finish A.
this will exit you from the application.and application will start from A not from C.
You will need to detect that Android has killed your process and then after that the user has returned to the app (causing Android to create a new process). I've described how to do this in numerous answers:
https://stackoverflow.com/a/29802601/769265
https://stackoverflow.com/a/27276077/769265
https://stackoverflow.com/a/11249090/769265
You can put android:clearTaskOnLaunch="true"
in your manifest for activity A to have the launcher always go to that activity.
Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background.
Except that when your phone is running low on resources like memory it will start to close apps that are running in the background, so that your phone has enough resources for what you're trying to do now. Games are often amongst the first apps the phone will "kill" to save resources as they often use a lot more memory and CPU than other apps. This is why sometimes your game is stil lrunning paused, and sometimes Android has closed it for you.
if your app is killed by the system i dont think it will start from c. If you are killing it through an task killer app then its a mistake. Force stop it from the app settings and then check.However if killed by task killer app and then from C if you get back to B and it is crashing then check for result code. if resultcode != RESULT_OK then you can handle your code here and save app from crash. if you have not started you activity for result then finish B and A before launching B and c.
I know this is an old post and as David Wasser's answer above has been the best solutions in the past... there is now a better way of doing things.
The better way to do all of this using ProcessLifecycleOwner
, which is designed for just this. In short and right from the JavaDoc:
"Class that provides lifecycle for the whole application process."
If your application is killed, the Application lifecycle onDestroy would be called, which allows you to creatively track this how you wish. It could be as simple as setting a SharedPreference flag that you can check when the application restarts incorrectly.
Please note, you need to add appropriate
android.arch
dependencies to take advantage of the newLifecycleObserver
,ProcessLifecycleOwner
, etc.
Below is an example of how you can handle each LifecycleEvent for the whole Application.
public class MyApplication extends Application implements LifecycleObserver {
@Override public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate(){
// ... Awesome stuff
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart(){
// ... Awesome stuff
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume(){
// ... Awesome stuff
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
// ... Awesome stuff
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop(){
// ... Awesome stuff
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestory(){
// ... Awesome stuff
}
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny(){
// ... Awesome stuff
}
}
Here is a nice article where I learned about this along with Kotlin examples and a few more tips beyond this post: Background and Foreground events with Android Architecture Components