Android exits the general class of all activity when exiting, Activitymanager

Source: Internet
Author: User

When you do an Android project, when you exit the program, sometimes you cannot quit all activity, and then we need to close all activities when we exit. Idea: When activity is created, the current activity object is stored, and all activity is closed until it exits. To do this, a MyApplication class is written to store all the activity. The code is as follows:

Import Java.util.linkedlist;import java.util.list;import android.app.activity;/** * allows you to exit all Activity when exiting a program. and the General class written * @author Duanyanrui * @date 2012-9-9 * */public class Activitymanager {private list<activity> activitylist = new Linkedlist<activity> ();p rivate static Activitymanager instance;private Activitymanager () {}// Get unique MyApplication instance public static Activitymanager getinstance () {if (null = = Instance) {instance = new Activitymanag in singleton mode ER ();} return instance;} Add activity to container public void addactivity (activity activity) {Activitylist.add (activity);} Traverse all Activity and finishpublic void exit () {for (Activity activity:activitylist) {activity.finish ()} System.exit (0);}}

Usage:

Add Activitymanager.getinstance () to the Oncreat method of the activity. Addactivity (this);

Called when exiting:activitymanager.getinstance (). exit ();

Android exits the general class of all activity when exiting, Activitymanager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.