How to destroy Activity, how to destroy multiple activities at a time, and how to destroy activity

Source: Internet
Author: User

How to destroy Activity, how to destroy multiple activities at a time, and how to destroy activity
In normal development, three activities are opened: A, B, and C. You need to click the "back" button in the android device, to exit the main interface (that is, three operations are required to destroy these three activities ). So how can we destroy the three activities once by clicking back? The procedure is simple as follows: (1) Create an ActivityCollector. java, which is used to collect and destroy activities.

public class ActivityCollector {    public static List<Activity> activities = new ArrayList<Activity>();    public static void addActivity(Activity activity) {        activities.add(activity);    }    public static void removeActivity(Activity activity) {        activities.remove(activity);    }    public static void finishAll() {        for (Activity activity : activities) {            if (!activity.isFinishing()) {                activity.finish();            }        }    }}
(2) create a BaseActivity. java base class. All activities in the project inherit this class.
public class BaseActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        ActivityCollector.addActivity(this);    }    @Override    protected void onDestroy() {        // TODO Auto-generated method stub        super.onDestroy();        ActivityCollector.removeActivity(this);    }
<span style="font-family: Arial, Helvetica, sans-serif;">}</span>
(3) Let the three activities A, B, and C inherit the BaseActivity respectively. java. in this way, as long as the activity is moved together, or the onDestroy () method is rewritten in the Child activity, the child activity will automatically collect the activity and destroy the Activity. (4 ). to destroy all the activities at a time, you only need to call ActivityCollector. the finishAll () method in java. for example, to call the loginout Method on any interface of the app, you need to kill multiple activities at a time:
public void handleMessage(Message msg) {            super.handleMessage(msg);            switch (msg.what) {            case ConstantUtil.LOGINOUT:                                ActivityCollector.finishAll();                break;
}
}

The above code is very simple, but it is very suitable.










Related Article

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.