Android program exit perfect solution compatible with all SDK
- 2020-05-07 20:23:28
- OfStack
Learn a lot about how to quit Android and Activity on the Internet. A lot of methods are not working, tried a variety of methods, the following method is my favorite, simple and easy to understand.
Use the singleton pattern to create an Activity management object, in which there is an Activity container (specific implementation of their own processing, using LinkedList, etc.) is specifically responsible for storing each newly opened Activity, and easy to understand, easy to operate, very good!
MyApplication class (stores every Activity and implements the closing of all Activity operations
Add the Activity to the MyApplication object instance container in the onCreate method per Activity
Use the singleton pattern to create an Activity management object, in which there is an Activity container (specific implementation of their own processing, using LinkedList, etc.) is specifically responsible for storing each newly opened Activity, and easy to understand, easy to operate, very good!
MyApplication class (stores every Activity and implements the closing of all Activity operations
public class MyApplication extends Application {
private List<Activity> activityList = new LinkedList<Activity>();
private static MyApplication instance;
private MyApplication()
{
}
// Get only in the singleton pattern 1 the MyApplication The instance
public static MyApplication getInstance()
{
if(null == instance)
{
instance = new MyApplication();
}
return instance;
}
// add Activity Into the container
public void addActivity(Activity activity)
{
activityList.add(activity);
}
// Iterate over all Activity and finish
public void exit()
{
for(Activity activity:activityList)
{
activity.finish();
}
System.exit(0);
}
}
Add the Activity to the MyApplication object instance container in the onCreate method per Activity
MyApplication.getInstance().addActivity(this);