How to properly clear intent data from singleTop A

2019-04-28 05:11发布

问题:

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...

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 remove this? I tried starting the same activity from within with a new Intent but no luck... I have also used various flags.

回答1:

Well turns out that there isn't a proper way to clear intent data from the ActivityManager. The only way around this bug is to keep a flag state for when extras have been cleared. Please see the following post about keeping a flag. https://groups.google.com/forum/#!topic/android-developers/vrLdM5mKeoY



回答2:

I'm sending pending intents as alarms and I could receive differents alarms when my app is launched so the flag state doesn't suit me.

I did like this :

I'm sending intents with a id, for example intent.setAction("MyIntent"+System.getCurrentTimeinMillis); storing this key as a reference in BD or shared prefs.

When you receive the intent on newIntent you ask the bd or shared prefs is the intent.getAction() is there and after that you clear the reference.