Suppose I have an activity A that starts activity B and kills itself. Now I press the Home button (or leave the app some other way) (causing onPause->onStop for activity B) and I click the app icon in the launcher again. What happens is that activity A is started again (of course because I specified that intent-filter in the manifest XML). Is there an easy way to just get back to activity B without starting activity A again? I basically want to get to onStart->onResume in activity B when I reopen the app.
My intent-filter looks like this:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Maybe I have a workaround solution.
How about using a transparent activity as the launcher?
When B pause or stop, remember it (in file, db etc.) In the launcher activity, something like a welcome screen but with a transparent view, read the record, and start the recorded activity.
Another solution, but not sure if it works.
Write a broadcast receiver with a launcher filter, and then launch the recorded activity.
One strategy here could be to save a SharedPreference, flagging when you want to go straight to B. This Android doc talks about using SharedPreferences, with some code examples.
Essentially, when you want to return straight to B, you create a shared preference indicating this. Then, when A restarts (say, because it was killed by the OS as discussed in the comments on the question), rather than immediately setting the associated view, etc, you first check the flag. If the flag is on, you start B. There is some messiness here, but it could potentially be handled in a very clean way, I think.
来源:https://stackoverflow.com/questions/9706844/want-to-get-back-to-last-activity-after-pressing-launcher-icon-for-my-app