2

My application is launched using a tag, and based on the information contained in tag, it further proceeds. Now my app can also be started by using touching icon, and later it asks user to touch the tag. Small flow would be as below.

enter image description here

So MainActivity may contains tag data(if started from TagProcessorActivity), or may not contain data (if started from icon launch). Data is passed as intent extra value from TagProcessorActivity to IconLaunchActivity then to MainActivity. After main activity, app operation proceeds. When I leave the main activity, all my previous activities gets finish. I have checked onDestroy() is called for each activity. Now if I logout after MainActivity, (Logout simply a feature that closes all existing activity), and relaunch my application from recent app, my tag details still appears in MainActivity, which I dont know why.

To make is more clear my questions are:

1) Why activity which was destroyed still contains the information from previous launch.

2) I know about removeExtra() method, but is there some better options to tackle this problem.

3) and none the less, is there some thing wrong in my code or android is keeping that instance of intent extra?

PS: Not clear which piece of code to post, so if required feel free to ask for code.

Android
  • 3,828
  • 9
  • 46
  • 79
  • hi did you solved the issue currently i am facing this problem.I dont know how to clear the intent for the activity from history that data still exist – prasanthMurugan Nov 25 '17 at 06:21

2 Answers2

1

Applications never exit in Android. onDestroy only destroys the activity, not any static variables left in the app. These will keep their value the next time an Activity is launched. This can be combined with some other features (like launching from the recent tasks menu causing you to launch the same intent) and this is the behavior you will get. The answer I always used was to detect this case (by checking the intent, there's a field that says if this is a restart or fresh), and ignoring the intent extras if so.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Okk, So do I need to check this restart or refresh for all activity every time its created, or some better way to tackle this like some flag which always create new intent, ignoring previous one (if created) or destroying earlier and recreating a new intent. – Android Jun 02 '14 at 09:59
0

A finished task launched from Recents (as opposed to home screen launcher icon) will receive the old Intent, including its Extras, Data, etc. There is a way to know that it was launched from Recents so you can handle the Intent appropriately.

protected boolean wasLaunchedFromRecents() {
    return (getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY;
}

In my humble opinion, that flag is poorly named (other flags referencing the Recents list actually use that word, e.g. FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS, FLAG_ACTIVITY_RETAIN_IN_RECENTS) and the documentation was never updated to reflect the fact that many popular Android devices have a dedicated button for Recents:

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

bkDJ
  • 750
  • 7
  • 10