Discussion:
How to properly clear intent data from singleTop Activity?
Jona
2011-04-19 15:58:45 UTC
Permalink
Scenario is:

Activity with singleTop declared at the manifest. This activity is called
with some extras. I'm able to clear the intent data and be able to flip
screen and such without the original intent being called using
setIntent(newIntent).

The issue is that when the activity is killed by the system and the user
goes back to this activity it gets restarted using the original intent used
to create the activity.
How do I resolve this issue? I tried starting the same activity from within
with a new Intent but no luck... I have also used various intent start
activity flags.

Thanks,
- Jona
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Streets Of Boston
2011-04-19 16:09:12 UTC
Permalink
Try to use onSaveInstanceState to record the fact that you have 'nulled' out
some of the extras.
Then in onCreate, examine its 'Bundle savedInstanceState' parameter. If it
is not null, it is the Bundle you returned in the onSaveInstanceState
earlier. From this Bundle, you can decide to ignore these extras or not.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Jona
2011-04-19 16:16:06 UTC
Permalink
Well, doing what you are saying leaves me with an issue... How do I than
know when it's a legit incoming intent?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Dianne Hackborn
2011-04-19 16:36:59 UTC
Permalink
Because onNewIntent() will be called and you can update your state at that
point.
Post by Jona
Well, doing what you are saying leaves me with an issue... How do I than
know when it's a legit incoming intent?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Dianne Hackborn
Android framework engineer
***@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails. All such
questions should be posted on public forums, where I and others can see and
answer them.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Jona
2011-04-19 17:48:35 UTC
Permalink
You are completely right... :) Now I see a way out of this issue... But I'm
still thinking why does Android work like this? There should be a way to
tell the OS to clear the intent data from the ActivityManager.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Dianne Hackborn
2011-04-19 18:55:30 UTC
Permalink
It was a mistake to have the setIntent() method. The Intent is always the
original intent used to launch the activity, nothing more. This is
immutable.
Post by Jona
You are completely right... :) Now I see a way out of this issue... But
I'm still thinking why does Android work like this? There should be a way to
tell the OS to clear the intent data from the ActivityManager.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Dianne Hackborn
Android framework engineer
***@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails. All such
questions should be posted on public forums, where I and others can see and
answer them.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Zsolt Vasvari
2011-04-20 09:25:12 UTC
Permalink
It was a mistake to have the setIntent() method.  The Intent is always the
original intent used to launch the activity, nothing more.  This is
immutable.
Can you deprecate it and throw an exception if the target SDK is > 11?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Streets Of Boston
2011-04-19 16:47:28 UTC
Permalink
public YourActivity extends Activity {

...
private boolean extrasClearedOut;
...

public void onCreate(Bundle savedInstanceState) {
...
...
if (savedInstanceState != null &&
savedInstanceState.getBoolean("extras_cleared_out", false)) {
extrasClearedOut = true;
}
...
Intent intent = getIntent();
if (extrasClearedOut) {
// ignore the extras in the intent.
...
}
else {
// Read and use the extras in the intent.
}
}

protected void onNewIntent (Intent intent) {
super.onNewIntent(intent);
setIntent(intent);

Intent intent = getIntent();
if (extrasClearedOut) {
// ignore the extras in the intent.
...
}
else {
// Read and use the extras in the intent.
}
}

...
...

public void someMethod(...) {
...
...
extrasClearedOut = true;
}

protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("extras_cleared_out", extrasClearedOut);
}

...
...
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Streets Of Boston
2011-04-19 16:48:29 UTC
Permalink
public YourActivity extends Activity {

...
private boolean extrasClearedOut;
...

public void onCreate(Bundle savedInstanceState) {
...
...
if (savedInstanceState != null &&
savedInstanceState.getBoolean("extras_cleared_out", false)) {
extrasClearedOut = true;
}
...
Intent intent = getIntent();
if (extrasClearedOut) {
// ignore the extras in the intent.
...
}
else {
// Read and use the extras in the intent.
}
}

protected void onNewIntent (Intent intent) {
super.onNewIntent(intent);
setIntent(intent);

if (extrasClearedOut) {
// ignore the extras in the intent.
...
}
else {
// Read and use the extras in the intent.
}
}

...
...

public void someMethod(...) {
...
...
extrasClearedOut = true;
}

protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("extras_cleared_out", extrasClearedOut);
}

...
...
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Jona
2011-04-19 17:49:28 UTC
Permalink
Thanks man! It has helped understand this much better and now things are
working as expected! :)
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
String
2011-04-20 07:02:51 UTC
Permalink
I've been bothered by this issue myself for a while. Streets' solution works
in the scenario where the system kills the activity in the background, but
not when the user exits the activity with the Back button, then later
returns to it by a long-press of Home: the "Recents" list re-launches the
app with the last-used Intent, and savedInstanceState isn't preserved. If
the Intent in question was meant to be a one-shot (it has Extras which have
been "used up"), then you're back in the same, unwanted situation.

Any thoughts? I could certainly save something to SharedPrefs which would
work around this, but it seems like overkill.

String
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Kostya Vasilyev
2011-04-20 09:17:09 UTC
Permalink
You could override onResume as well, have it check a flag set in onCreate to
distinguish between the first original callback sequence and subsequent
ones, and clear some data values.
Post by String
I've been bothered by this issue myself for a while. Streets' solution works
in the scenario where the system kills the activity in the background, but
not when the user exits the activity with the Back button, then later
returns to it by a long-press of Home: the "Recents" list re-launches the
app with the last-used Intent, and savedInstanceState isn't preserved. If
the Intent in question was meant to be a one-shot (it has Extras which have
been "used up"), then you're back in the same, unwanted situation.
Any thoughts? I could certainly save something to SharedPrefs which would
work around this, but it seems like overkill.
String
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Loading...