You probably don't want to have the app restart from the beginning when being launched from the "recent tasks" list, because your app may be perfectly capable of working. What you need to do is you need to remember if your app has been properly "initialized" (whatever that means). If the user returns to your app and the process has been killed and restarted since your app was initialized, you need to detect this condition and then redirect the user back to the first activity of your app.
The best way to do this is to have a base class for all of your activities. In this base class you implement code in onCreate()
that checks if your app has been properly initialized or not. If it has not been properly initialized, you should redirect the user back to the first activity. Something like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check if the application has been restarted by AndroidOS after it has killed the process due to inactivity
// in this case, we need to redirect to the first activity and dump all other activities in the task stack
// to make sure that the application is properly initialized
if (!isApplicationInitialized() && !(this instanceof FirstActivity)) {
Intent firstIntent = new Intent(this, FirstActivity.class);
firstIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // So all other activities will be dumped
startActivity(firstIntent);
// We are done, so finish this activity and get out now
finish();
return;
}
// Now call the activity-specific onCreate()
doOnCreate(savedInstanceState);
All of your activities need to inherit from this base class and they should NOT override onCreate()
. Instead, they should implement the method doOnCreate()
, which will be called from the base class' onCreate()
(see above).
NOTE: This only works if the root activity (in this example FirstActivity
) is never finished until the app quits. This means that you will always have an instance of FirstActivity
at the root of your task. This is required so that Intent.FLAG_ACTIVITY_CLEAR_TOP
works correctly.
(doo dah)
boolean
variable is set to true inonCreate()
of your apps'sApplication
class, then it will always be true. Because that method is always called before any of your application's activities are created. – Idle