Android: Starting app from 'recent applications' starts it with the last set of extras used in an intent

倾然丶 夕夏残阳落幕 提交于 2019-11-27 22:57:31

As Martijn says, you can check if your application is opened using an Intent with the flag FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY set, like this:

int flags = getActivity().getIntent().getFlags();       
if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
    // The activity was launched from history
}

I think you can distinguish a "normal" startup from a "recent applications" startup by checking the Intent flags; there is a flag called

Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

which, according to the documentation:

This flag is not normally set by application code, but set for you by the system if this activity is being launched from history (longpress home key).

So when this flag is set, you could choose to ignore the extras.

You should clear the extras from your intent after using them.

Bundle extras = getIntent().getExtras();
if (extras == null) {
    return;
}
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
extras.clear();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!