How to properly clear intent data from singleTop Activity?

Showing 1-12 of 12 messages
How to properly clear intent data from singleTop Activity? Jona 4/19/11 8:58 AM

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
Re: How to properly clear intent data from singleTop Activity? Streets Of Boston 4/19/11 9:09 AM
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.
Re: How to properly clear intent data from singleTop Activity? Jona 4/19/11 9:16 AM
Well, doing what you are saying leaves me with an issue... How do I than know when it's a legit incoming intent?
Re: [android-developers] Re: How to properly clear intent data from singleTop Activity? Dianne Hackborn 4/19/11 9:36 AM
Because onNewIntent() will be called and you can update your state at that point.

On Tue, Apr 19, 2011 at 9:16 AM, Jona <medica...@gmail.com> wrote:
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-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
Dianne Hackborn
Android framework engineer
hac...@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.

unk...@googlegroups.com 4/19/11 9:47 AM <This message has been deleted.>
Re: How to properly clear intent data from singleTop Activity? Streets Of Boston 4/19/11 9:48 AM
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);
  }
  
  ...
  ...
}
Re: [android-developers] Re: How to properly clear intent data from singleTop Activity? Jona 4/19/11 10:48 AM
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.
Re: How to properly clear intent data from singleTop Activity? Jona 4/19/11 10:49 AM
Thanks man!  It has helped understand this much better and now things are working as expected! :)
Re: [android-developers] Re: How to properly clear intent data from singleTop Activity? Dianne Hackborn 4/19/11 11:55 AM
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.

On Tue, Apr 19, 2011 at 10:48 AM, Jona <medica...@gmail.com> wrote:
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-d...@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Re: How to properly clear intent data from singleTop Activity? String 4/20/11 12:02 AM
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
Re: [android-developers] Re: How to properly clear intent data from singleTop Activity? Kostya Vasilyev 4/20/11 2:17 AM

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.

20.04.2011 11:03 пользователь "String" <sterlin...@googlemail.com> написал:
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-d...@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
Re: How to properly clear intent data from singleTop Activity? Zsolt Vasvari 4/20/11 2:25 AM
> 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?