SoFunction
Updated on 2025-04-11

How to implement Android exit application

How to implement Android exit application

Android exit application will be called(()) or (0),This only takes effect when the first activity (that is, the activity at the entrance). If there are three activities A, B, and C, and if you want to exit the Activity in B or C, calling the above method will often destroy the current Activity and return to the previous Activity. Of course, you can also return to the previous Activity one by one until you jump to the entrance Activity and finally exit the application
sequence. But this is more troublesome, and the experience returned one by one is not friendly. The following method is to write a SysApplication class, use singleton mode to manage the activity, call the().addActivity(this) method in each onCreate() method of the Activity, and call it when exiting.().exit() method, you can exit the application completely.

import ; 
import ; 
import ; 
import ; 
 
public class SysApplication extends Application{ 
 private List<Activity> mList = new LinkedList<Activity>(); 
 private static SysApplication instance; 
  
 private SysApplication() 
 { 
 } 
 public synchronized static SysApplication getInstance() 
 { 
 if(null == instance) 
 { 
  instance = new SysApplication(); 
 } 
 return instance; 
 } 
 //add Activity 
 public void addActivity(Activity activity) 
 { 
 (activity); 
 } 
  
 public void exit() 
 { 
 try{ 
  for (Activity activity : mList) 
  { 
  if(activity != null) 
   (); 
  } 
 }catch (Exception e){ 
  (); 
 }finally{ 
  (0); 
 } 
 } 
 public void onLowMemory(){ 
 (); 
 (); 
 } 
} 

Called in onCreate for each activity().addActivity(this);

Then call it when exiting the application().exit();

Thank you for reading, I hope it can help you. Thank you for your support for this site!