SoFunction
Updated on 2025-03-08

Android implements a way to close all activities when exiting

This example implements the function of closing all activities when Android exits, and is shared with you for your reference. The specific methods are as follows:

Generally speaking, when Android exits, some activities may not be closed. In order to close all activities when Android exits, the following classes are given here:

//Close the Activity classpublic class CloseActivityClass{ 
 
  public static List<Activity> activityList = new ArrayList<Activity>();
 
  public static void exitClient(Context ctx)
  {
   // Close all activities   for (int i = 0; i < (); i++)
   {
     if (null != (i))
     {
       (i).finish();
     }
   }
   ActivityManager activityMgr = (ActivityManager) (Context.ACTIVITY_SERVICE );
   (());
   (0);
  }
}

Then add a sentence to the onCreate(Bundle savedInstanceState) method of each activity in the application:

(this);

When the application exits:

();

This way, when exiting the app, all activities of the app can be completely closed.

I believe that the description in this article has certain reference value for everyone's Android programming design.