tags:

views:

70

answers:

2

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)?

A: 

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
Josef
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: 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
A: 

This is the default behaviour and this question has been asked several times before: http://stackoverflow.com/questions/2061143/android-keep-tasks-activity-stack-after-restart-from-home
http://stackoverflow.com/questions/2122707/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.

Christopher
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