Android Run application from last Activity

2019-06-17 07:13发布

问题:

I have an application with several Activities. My A Activity has the Manifest Intent filter parameters: action.MAIN and category.LAUNCHER. after its being loaded I call Activity B and finish() A since I don't use it anymore.

After I run my application, go from Activity A to B and press the Home button, when I relaunch it from the applications menu or from the Market app for ex.(not by a long press on the Home button), it starts from the A Activity and do not save its last Activity B.

I definitely know that this is possible to relaunch an application from its last Activity (some application from the Market do support it) and I think that this can be determined by the Manifest parameters but I don't know which one.

does anyone know how to implement it so my application can relaunch from its last Activity B?

Thanks ayanir

回答1:

Though I know this is an old question, I was struggling with this very issue and couldn't find an answer on SO. So, here is my (very newbie) answer:

No, I do not think it's possible to do this by messing with the manifest - you can only launch one fixed activity per app from the home screen. What you can do, though, is launch whatever activity you want from that starting point, and Android can do it quickly enough that you never see the first one.

Though this feels very much like a hack, I implemented this routing in the starting activity's onResume() method, and used sharedPreferences to keep track of which activity to launch:

    final Class<? extends Activity> activityClass;
    SharedPreferences prefs = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
    int activityID = prefs.getInt("whichActivity", -1);
    if (activityID  == Constants.ACTIVITY_ID_MAINSCREEN) {
        activityClass = MainScreen.class;
    } else {
        activityClass = null; return;
    }
    Intent newActivity = new Intent(this, activityClass);
    this.startActivity(newActivity);


回答2:

There have been a number of very similar questions lately. It's a good idea to search the site first to ensure that duplicate questions don't get asked.

For example, the question linked below says that this behaviour was happening because the developer was starting their application using the Eclipse debugger. Another person was having this problem because they were launching the application directly from Eclipse, rather than starting cleanly by manually pressing the launcher icon.

Android: keep task's activity stack after restart from HOME



回答3:

so there are a few things to consider when developing Apps in Android. And one of the big things is the Application Lifecyle, if you haven't yet then I would suggest this video. What happens is that an application can be killed and reset at any point in time and you need to save the state of your application so that you can restore it at any time. If you open your App from the Launcher you will always go into the Activity that starts the app, if you want to skip to the next Activity you need to store that information and then jump to the Activity in your code.
Also have a look at this documentation about SavingPersistentState