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

3

14

Bit of a confusing problem for me here:

I've got a home screen widget which, when clicked, starts my main app Activity with a few extras put in the intent:

Intent start = new Intent(context, Main.class);
start.putExtra("action", "showXYZ");
start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(start);

This all works fine, it starts my activity and my activity receives the extras as expected. It processes these extras and starts another activity.

Once a user has clicked the home screen widget and started the Main activity in this way, going into the app through the 'Recent applications' method (holding down the 'home' key) starts the Main activity with the extras - causing processing to happen which I don't want (and leading to the second activity to open, rather than for the Main activity to just be shown).

Is there any work-around for this? When starting the app from the 'recent applications' method, I want to simply start the Main activity without the last set of extras.

Many thanks for the help! r3mo

Note: I'm on android 1.5

EDIT:

Found a workaround here: Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?

I'm going to timestamp the intent being set off by the widget, and check that the timestamp is recent in Main.java. If it is, i'll proceed with the processing. If not, i'll just show the Main.java activity.

Keen to hear if there are any official solutions to this.

Regularly answered 1/2, 2011 at 17:54 Comment(0)
M
43

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
}
Methadone answered 3/4, 2012 at 20:0 Comment(1)
Unfortunately this flag is not reliably set when it should be.Humbug
W
11

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.

Winser answered 11/4, 2011 at 15:12 Comment(0)
R
0

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();
Radcliffe answered 1/2, 2011 at 18:6 Comment(1)
Thanks for the suggestion. I've just tried this in Main.java (which is started by the widget) and this doesn't seem to make any difference. Would this not just be clearing the local variable 'extras'? I believe the problem lies in how Android is starting the 'recent application' activity with the last set of extras passed to it.Regularly

© 2022 - 2024 — McMap. All rights reserved.