You are holding a static reference to an activity. This will never go away unless you clear it manually from somewhere -> you are creating a memory leak!
– DArkOApr 30 '12 at 9:02

5

Well its ok, you have my +1 for out-of-the-box thinking :D, i would just change to singleTop. however it still won't be my first choice when doing something similar. by cleanup code i meant a way to get rid of the static reference so there is no memory leaking, but that depends on the app structure.
– DArkOApr 30 '12 at 9:30

5

Please no one use the first solution, it is completely wrong. You are bypassing the Android framework in a really hacky way whilst creating a zombie object. Use a BroadcastReceiver.
– S-K'Mar 18 '14 at 13:45

1

Awesome solutions. Worked very well for my problem in which I was going from activities A->B->C and on onBackPressed of C, I was launching new instance of B. But then if I did onBackPressed on that new instance of B, it used to go to the old instance of B instead of old instance of A (what I wanted). So I followed your first method in the onBackPressed of C, and it gave me the behaviour I wanted :) Thanks a ton :)
– SoulRayderJun 13 '14 at 5:23

Step3: If the user clicks on Add button start Activity A using the FLAG_ACTIVITY_CLEAR_TOP.Also, pass the flag in extra. FLAG_ACTIVITY_CLEAR_TOP will clear all the opened activities up to the target and restart if no launch mode is defined in the target activity

What you need is to add the Intent.FLAG_CLEAR_TOP. This flag makes sure that all activities above the targeted activity in the stack are finished and that one is shown.

Another thing that you need is the SINGLE_TOP flag. With this one you prevent Android from creating a new activity if there is one already created in the stack.

Just be wary that if the activity was already created, the intent with these flags will be delivered in the method called onNewIntent(intent) (you need to overload it to handle it) in the target activity.

Then in onNewIntent you have a method called restart or something that will call finish() and launch a new intent toward itself, or have a repopulate() method that will set the new data. I prefer the second approach, it is less expensive and you can always extract the
onCreate logic into a separate method that you can call for populate.

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).