Is it possible to configure an android app so that if a user has opened your app, launched numerous activities, then returns to the home screen and relaunches your app again, instead of going to the main activity they will instead be taken to the activity highest on the stack (the most recent activity in your app)?
-
Better solution and cleaner : http://stackoverflow.com/a/10598619/5494342 – Mo12 Dec 01 '15 at 22:53
-
For the record, the previous comment is debatable. You could also say the answer in that link is inelegant although possibly acceptable, and comes with the catch that it won't work 100% of the time. See the comments in the accepted answer of this question before you decide which solution works for your situation. – uɥƃnɐʌuop Sep 18 '17 at 20:02
3 Answers
When launched via icon on the home screen, Android will always start the activity with the android.intent.action.MAIN
filter in your AndroidManifest.xml
, unless the application is already running (in which case it will obviously restore the activity on top of the stack).
To achieve what you described you can simply store the last visible activity in SharedPreferences
and have a Dispatcher activity that starts the last activity according to the preferences.
So in every activity you want to re-start automatically:
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("lastActivity", getClass().getName());
editor.commit();
}
And a Dispatcher activity similar to the following:
public class Dispatcher extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Class<?> activityClass;
try {
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(
prefs.getString("lastActivity", Activity1.class.getName()));
} catch(ClassNotFoundException ex) {
activityClass = Activity1.class;
}
startActivity(new Intent(this, activityClass));
}
}
Remarks
- You could create a base class for the
onPause
override - The Dispatcher activity obviously needs to be the
android.intent.action.MAIN
action

- 74,165
- 16
- 97
- 99
-
Thank you for this. I have seen other answers that now tell me that what I am looking for is actually the default behaviour and it was only the fact that I was using eclipse (and not yet updated to 0.9.6 of the plugin) that was causing my problem. So I would think that I do not have to incorporate your solution. Is that correct? Or is there another reason I would need to programmatically follow your idea? – JohnRock Mar 14 '10 at 17:45
-
2@johnrock: Android builds a stack that is used to find the last visible activity *while your app is running*. Unfortunately you never know when your app is killed so if you *always* want to restore the last activity (even under memory pressure, after reboot, etc.) then you need something similar to what I proposed. If you're ok with *most of the time*, you don't. – Josef Pfleger Mar 14 '10 at 18:38
-
Hi, just wanted to add that the intended behavior does not work as it is supposed not even "most of the time". I've described the problem here http://stackoverflow.com/questions/6903761/after-home-button-press-re-launching-app-shows-intial-activity-not-current/6903958#6903958 – source.rar Aug 06 '11 at 18:58
-
3where should I put the Dispatcher? inside the same activity I want to re-start? How should I use that code? I'm still not very sure – xialin Feb 04 '14 at 05:03
-
@xialin Dispatcher should be its own activity in its own file, eg. DispatcherActivity.java. The activities you want to be able to restart (which also should be in their own files) should have the onPause as in the example. – riper Feb 16 '15 at 08:55
-
2
-
1
-
1Activity1 is just the default Activity that should start if no activity is saved in the preferences, i think. It would also be the first Activity after launching for the first time. – NoilPaw Apr 27 '16 at 10:00
-
-
`unless the application is already running (in which case it will obviously restore the activity on top of the stack).` - This does not work, when the first Activity in row has `android:launchMode="singleTask"`. Every other Activity will not restored then as well. I expected the behaviour the OP wanted to achieve, but it did not work for me. Nevertheless, removing singleTask was the solution for me. – Bevor Mar 09 '19 at 18:25
It's not that complex. You just need to manipulate the manifest.
AndroidManifest.xm
<activity
android:name=".MainActivity"
android:alwaysRetainTaskState="true"
android:exported="true"
.
.
.
Read about the 'android:exported' & 'android:alwaysRetainTaskState' here:

- 2,002
- 1
- 29
- 31
This is the default behaviour and this question has been asked several times before:
Android: keep task's activity stack after restart from HOME
Android Run application from last Activity
Note that if you're launching your application from Eclipse, that's what breaks this default functionality. Changing your launch configuration to launch no activity should fix things.
However, as this behaviour was fixed in the 0.9.6 release of the ADT plugin for Eclipse in the past few weeks, you no longer need that workaround:
Applications launched from ADT now behave as if they were clicked from the Home screen.

- 1
- 1

- 110,418
- 27
- 198
- 193
-
That's only the case *when the application is already running*. Since you can never really know, I'd handle it myself. I edited my answer to clarify. – Josef Pfleger Mar 14 '10 at 15:50