问题
I have a main activity A which will call another activity B whose oncreate() is mentioned below. When I press home button while in activity B and press recent apps, the flag FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY is never set. I'm using a samsung nexus 10 (Android 4.4.2).
protected void onStart() {
super.onCreate(savedInstanceState);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0)
{
//Call Main Activity.
}
else
{
//Continue creating this activity
}
}
When I placed the if block in onCreate() it would never get called when I open my app from recent apps.
EDIT 1: Let me explain what I wanted to do. Whenever the user makes the application to go to the background (locking the screen, pressing home button etc), I want to call a particular activity (for authentication) when the app comes to the foreground.
回答1:
I think you are confused. If your main activity starts ActivityB, then ActivityB.onCreate() will be called and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY will not be set. Now, with ActivityB in the foreground, you press the HOME button. This task is pushed to the background. Later the user opens the list of recent tasks and selects your application. The task (with ActivityB on top) is simply brought to the foreground. ActivityB is not recreated, therefore Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY will still not be set. ActivityB.onCreate()` is not called again.
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY is only set if Android creates a new instance of your activity when the user selects the app from the list of recent tasks. In most cases, this will be when the user starts your application again (from the beginning) after all your activities have finished.
来源:https://stackoverflow.com/questions/24065146/flag-activity-launched-from-history-not-set-in-nexus-10-android-4-4-2