How to close all the activities of my application?

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

Activity 1 –> Activity 2 –> Activity 3 –> Activity 4

  • Android BLE Passive scan
  • Using Camera in the Android emulator
  • Android Studio Exclude Class from build?
  • Android Pushing Updates on Play Store
  • Understanding fragments backstack
  • Asynchronous programming best practices
  • And I would like to close all the activities from any activity and go back at home phone.

    Related posts:

    What is the difference between setDisplayHomeAsUpEnabled and setHomeButtonEnabled?
    Setting WiFi priority on Android Lollipop or later via Settings UI
    Overlaying the Action Bar not working
    TextInputLayout not showing EditText hint before user focus on it
    How to get a list of the activity history stack?
    How to obtain computation time in NDK
  • How to restrict content provider data across applications
  • ViewDragHelper: how to use it?
  • Why isn't my vector drawable scaling as expected?
  • Android Studio error: "Method getText() must be called from the UI Thread, currently inferred thread is worker
  • Target “${build.target}” does not exist in the project “LibA” when running Android's build.xml from Gradle
  • Getting WiFi Direct IP address of my device
  • 4 Solutions collect form web for “How to close all the activities of my application?”

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

    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 !

    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.

    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
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.