Application of Graceful Exit

Source: Internet
Author: User

There is a need to exit the app during the development process. My most common use is two ways, the first is to create a global container class to save the activity, and then walk through all the activity when exiting

The second way is to set up a broadcast and send an exit broadcast when I quit.

The first way to define baseactivity is the following code:

 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//Add Activ
    ity to Stack atycontainer.getinstance (). addactivity (this); @Override protected void OnDestroy () {Super.ondestroy ();//End activity& Remove the activity from the stack ATYC
    Ontainer.getinstance (). removeactivity (this); } class Atycontainer {private Atycontainer () {} private static Atycontainer instance = new Atycontainer (
    );

    private static list<activity> Activitystack = new arraylist<activity> ();
    public static Atycontainer getinstance () {return instance;
    public void addactivity (activity aty) {activitystack.add (aty);
    public void removeactivity (activity aty) {activitystack.remove (aty); /** * End all activity/public static void Finishallactivity () {for (int i = 0, size = Activit Ystack.size (); i < size; i++) {if (null!)= Activitystack.get (i)) {activitystack.get (i). Finish ();

        } log.d ("Call finfshallactivity", "-------------------");
    Activitystack.clear (); }

In Mainactivity, inherit baseactivity to implement the following code:

Private Boolean isexit = false;

@Override Public
boolean onKeyDown (int keycode, keyevent event) {

    if (keycode = = keyevent.keycode_back) {
        //   Toast.maketext (This, "Click two times to leave", Toast.length_short). Show ();
        Toquittheapp ();
        return false;

    } else {return
        Super.onkeydown (KeyCode, event);
    }
}

Package Toquittheapp method
private void Toquittheapp () {
    if (isexit) {

      finishallactivity ();

    } else {
        isexit = true;
        Toast.maketext (Mainactivity.this, "Click again to quit App", Toast.length_short). Show ();
        Mhandler.sendemptymessagedelayed (0, 3000);//3 seconds later send
    message
}

//Create Handler object to process message
Handler Mhandler = new Handler () {

    @Override public
    void Handlemessage (msg) {//Processing messages
        //TODO auto-generated method Stub
        super.handlemessage (msg);
        Isexit = false;
    }
;

This time can gracefully exit.


The second way to define baseactivity code is as follows

private static final String exitaction = "Action.exit";
Private Exitreceiver Exitreceiver = new Exitreceiver ();
@Override
protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Intentfilter filter = new Intentfilter ();
    Filter.addaction (exitaction);
    Registerreceiver (exitreceiver, filter);
@Override
protected void OnDestroy () {
    Super.ondestroy ();
    Unregisterreceiver (Exitreceiver);
}
public class Exitreceiver extends Broadcastreceiver {
    @Override
    the public void onreceive (context context, Intent Intent) {
        Toast.maketext (context, "broadcast way Out", Toast.length_short). Show ();
        BaseActivity2.this.finish ();
    }

It's also handled in Mainactivity.

@Override Public
boolean onKeyDown (int keycode, keyevent event) {
     exitreceiver exit=new exitreceiver ();
    Intent Intent = new Intent (intent.action_main);
    Intent.addcategory (intent.category_home);
     
    Exit.onreceive (mainactivity.this,intent);
    Return Super.onkeydown (KeyCode, event);
}

GitHub Address: Https://github.com/yuanchongzhang/activity_exit

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.