11

I've got an application with several activities, for example:

Activity 1 --> Activity 2 --> Activity 3 --> Activity 4

And I would like to close all the activities from any activity and go back at home phone.

Cristian
  • 198,401
  • 62
  • 356
  • 264
José Carlos
  • 2,850
  • 12
  • 59
  • 95
  • Please check my answer here: http://stackoverflow.com/questions/9426346/how-to-kill-all-activities-in-android-application/14393598#14393598 – Munim Jan 18 '13 at 08:47
  • I prefer this solution since it is simpler than the "KillReceiver" solution. – TommyTh Jan 29 '14 at 07:09
  • check my answer [here][1] i hope it to be helpfull [1]: http://stackoverflow.com/questions/14001963/finish-all-activities-at-a-time/24833606#24833606 – Dmila Ram Jul 18 '14 at 20:55

4 Answers4

23

You can achieve that by using BroadcastReceivers:

  • Create a BaseActivity like this:

public class BaseActivity extends Activity {
    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
  • 6
    Please do not advise people to use nonsense/illegal MIME types for an `Intent` or `IntentFilter`. Action strings can be made sufficiently unique, and you can use `setPackage()` on more recent versions of Android if you are concerned that the unique action string is insufficient. – CommonsWare Mar 27 '11 at 23:35
  • 3
    The action can be kill, but the example could be nicer if it used content://sparta as it would not crash :-) – neteinstein Mar 30 '11 at 16:39
  • +1 for you , Thanks Cristian your solution works for me and solved my problem very easily, BTW your should change the text "kill" & "spartan!!" :) – rajpara Oct 05 '12 at 10:19
  • What if some of my activities extend `Activity` and others `PreferencesActivity`? In this scenario I can't have a single BaseActivity. What is the most practical approach in this case? – Dzhuneyt Feb 03 '13 at 21:52
  • Since the code is too short you might want to create a `BasePreferencesActivity` class. If it were longer, what I would recommend is to [**delegate**](http://en.wikipedia.org/wiki/Delegation_pattern) the logic to a third class – Cristian Feb 04 '13 at 10:03
  • Please be careful with Runtime Exception because of Bad MIME type. – Saad Bilal Nov 29 '18 at 17:18
4

You can clear all the previous activities using the following flags :

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 

I hope it will help you !

tryp
  • 1,120
  • 20
  • 26
3

Open up AndroidManifest.xml and find the activity that you would like to return to and add the following attribute

android:launchMode="singleTask"

For example, HomeActivity class might have this in android manifest

<activity android:name=".HomeActivity"
    android:launchMode="singleTask"/>

At any point, you can close all activities on top of this one by using startActivity the standard way, for example

startActivity(new Intent(this, HomeActivity.class));

If you normally pass extras to the intent, there's no need to do this as it will come back in whatever state it was before, and it's even accompanied by an animation like hitting the back button.

Daniel Skinner
  • 210
  • 4
  • 10
2

And I would like to close all the activities from any activity

That is user-hostile in Android. Please do not do that.

and go back at home phone

I have no idea what this means.

If the user presses HOME, your activities can and should remain in RAM for a bit, in case the user returns to your application.

If by "home phone" you mean that one of your activities is the "home" of the application, and you wish to return to that activity when the user presses some button or options menu item, call startActivity() with an Intent that:

  • identifies the activity you want to return to
  • has FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP in the flags, to indicate that you want to bring that activity forward and get rid of the other activities that the user had previously launched
Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP it is not working in my case. Please help me. – Gaurav Arora Jan 31 '13 at 07:31
  • 2
    @BorntoWin: Click the "Ask Question" button in the upper right corner, and explain in your question *precisely* what "is not working in my case" means, both the "is not working" and the "in my case" parts. – CommonsWare Jan 31 '13 at 13:52