Launch existing singleTask Activity from launcher

2019-09-02 19:16发布

问题:

Scenario:

  • Activity A (MAIN and LAUNCHER in manifest) starts up when clicking on launcher icon.

  • In turn it launches Activity B.

  • Activity B then launches our main app Activity C (MAIN and singleTask in manifest).

Behaviour I require:

Once Activity C has been shown and the home key is then pressed, the next time the launcher icon is pressed I would like to skip straight to Activity C (and not show Activity A (and consequently B) again).

I have tried using FLAG_ACTIVITY_CLEAR_TOP from A, but I still get Activity A whenever I hit icon on launch screen.

Is appearance of my singletask Activity C from launcher achievable?

Update: Use of FLAG_ACTIVITY_CLEAR_TOP from A and not calling finish() creates the situation whereby Activity B appears on press of launcher icon. However, also applying use of FLAG_ACTIVITY_CLEAR_TOP from B and not calling finish() does not resolve situation. now I don't get A on launcher icon press, but get B. What gives!

回答1:

See similar scenario here.

In your case, I would recommend using a similar approach, where you would have a SharedPreference that would persist a value representing whether your app had previously been launched.

Create a simple Activity that will contain the logic for what you are trying to do here. In that activity's ("pre-A" Activity) onResume() method, check the value of the preference representing whether the app has ran previously.

If it has not previously been ran, launch an intent to Activity A, which calls the subsequent B and C activities; otherwise, launch an intent to Activity C.

Seems pretty straightforward, just remember to define the new "Pre-A" activity in your manifest!