1

I have an application in which i move from activity A->B->C->D. Now i want that when i press a button on D my stack should be A->D. i have done this from android API level 8 to 10 but on android 4.0 i have "do not keep activities" settings that kill my activities when in background thus making Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK useless as my activity a is not alive.

How can i handle this situation. Any help is greaty appriciated.

Susheel Tickoo
  • 508
  • 1
  • 6
  • 23

1 Answers1

0

Do not keep activities I think it is a developer option to simulate an escenario.

What I used to clean back stack, in your case only activities B and C, is to register a broadcast on that activities and launch an intent from D activity when the user presses the button.

public void onClick(View v){
 Intent broadcastIntent = new Intent();
 broadcastIntent.setAction("com.package.CLEAR_B_C");
 sendBroadcast(broadcastIntent);
}

So when B and C recieve the intent, call finish().

See that post and the answer from Francesco Laurita

Community
  • 1
  • 1
AlexBcn
  • 2,450
  • 2
  • 17
  • 28
  • i have already done this. but the issue with this approach is that i register an broadcast in onCreate() of B and C and i unregister it in onDestroy() so when B&C go in background wiht this developer option the ondestroy is called and the reciver is unregistered and hence the activities can not get the intent. – Susheel Tickoo Mar 21 '13 at 12:39
  • The same will happen in any application with this option enabled, it is only for developing purposes that simulate a real case when the android system kills your activities because he is running out of memory or whatever. It destroys every activitiy that you leave, in that case no intent will be recieved of course. – AlexBcn Mar 21 '13 at 13:07
  • so does that mean we can not have this functionality working in all scenarios? Because android can kill an activity anytime it runs low on memory. – Susheel Tickoo Mar 22 '13 at 07:55
  • Not in yours, all activity is destroyed when you are starting a new one, so no previous activity is saved on the back stack (you can see it using the command `adb shell dumpsys activity`) and you will be never able to mantain activities A->D. **Do not keep activities** it is usefull to test if your app will recover the data saved on an activity killed. A solution, in addition to what I posted before, can be to overwrite the function `onBackPressed()`and add an intent to A with Intent.FLAG_ACTIVITY_CLEAR_TOP. – AlexBcn Apr 06 '13 at 18:50