12

I've done qui a bit of reading and searching on SO, but can't find a way to clear the current activity stack. The context of my app is an activity started by a a background service / notification.

Imagine that my app allows you to organise a list of people. A few hours ago, you were viewing person X in the "View" activity, that's now the top of your stack. At some point in the future, the service triggers and I popup a new "Notify" activity for person Y. From there you can edit person Y's details.

When you finish this activity, it would be a confusing user experience to pop the stack and end up viewing person X. Ideally I'd like to go back to whatever the user was doing (email etc...), or at least to my app's home.

I tried starting "Notify" with FLAG_ACTIVTY_NEW_TASK but that doesn't seem to help: when the task finishes it simply goes back to the previous task. What I want seems to be Android 3's new FLAG_ACTIVITY_CLEAR_TASK, which doesn't exist in previous SDKs.

Does anyone have a suggestion to achieve that?

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Romain
  • 2,318
  • 1
  • 23
  • 31
  • 1
    You will probably find the IntentCompat class useful: http://stackoverflow.com/questions/19182233/how-to-use-intentcompat-makerestartactivitytask – Hassan Ibraheem Feb 17 '14 at 09:16

3 Answers3

20

Just kill'em all!

You can achieve that by using BroadcastReceivers:

  • Create a BaseActivity like this:

public class BaseActivity extends GuiceActivity {
    private KillReceiver mKillReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mKillReceiver = new KillReceiver();
        registerReceiver(mKillReceiver,
            IntentFilter.create("kill", "spartan!!!"));
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mKillReceiver);
    }
    private final class KillReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }
}

  • Make your activities extend that BaseActivity.
  • Whenever you want to clear the stack:

Intent intent = new Intent("kill");
intent.setType("spartan!!!");
sendBroadcast(intent);

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thanks Cristian. Since going back to the home is acceptable I think I'll go with the other suggestion which is slightly simpler. However this looks like a great solution if I wanted to leave nothing else running. – Romain Mar 01 '11 at 22:47
  • 3
    Works great when you want to start an activity that is not on the history stack while clearing the rest. The "spartan!!!" MIME type is funny, but will throw a RuntimeException: Bad MIME type. Should probably change to "text/plain" so the example works as is. :P Oh, and you should change the name of the kill intent to `maliciousIntent`. – dvs Sep 07 '11 at 18:22
  • 8
    This is completely wrong. It does not take into account how Android works. Let's say an Activity from the task is killed by the system (low memory for instance). This Activity won't be killed by the BroadcastReceiver because it has already been killed by the system. The problem is, it will remain in the back stack. – Cyril Mottier Feb 09 '12 at 18:04
  • It depends on what results you desire... for the single instance entry via a password gateway, this is a perfect workaround for the lack of stack functionality in pre-12 Android – Dan Mar 09 '12 at 02:25
6

If you already have an instance of your app's home running in the stack, when activity Y finishes you could start your app's home (using startActivity()) with the flag FLAG_ACTIVITY_CLEAR_TOP. As it's already on the stack, instead of creating a new instance of it, this would bring you back to your app's home and clear the stack on top of it.

Rodrigo Chiossi
  • 591
  • 5
  • 8
  • Thanks! I was so focused on the FLAG documentation I completely overlooked this. I guess if the home activity i not started it will simply start a new one which is not really a problem. This should definitely do it. – Romain Mar 01 '11 at 22:42
  • I just want to add that calling `startActivity(HOME)` from `onStop()` resulted in some flickering. Instead, I call it before the call to `finish()` and also in `onBackPressed()`. – Romain Mar 02 '11 at 01:38
  • I had a hard time myself fighting the activity stack, I know it's not easy to get our application as we want. As for the flickering, never noticed it before... good addition though! – Rodrigo Chiossi Mar 02 '11 at 12:08
0

Take a ArrayList and save all activities objects into arraylist in oncreate() of every activity. Whenever you want to finish particular activity ,just retrieve that activity instance from arraylist and finish that.