Android Exit program (bottom)--use broadcast mechanism

Source: Internet
Author: User

Overview:

Based on the previous blog, "Android Exit Program (top)-Singleton mode ", we learned how to use Singleton and loop traversal to exit our application. This blog is going to solve the problem from another angle--broadcast. That is, when you receive a broadcast that closes the activity, you close the current activity.


class diagram display and description:


From the class diagram above you can see that there is a processing, and this processing is to learn the object-oriented coder must be mastered. This is an extended--baseactivity of activity. If we want to add an internal exitappreceiver class to each activity, this will increase the amount of code, and the latter is not good maintenance. So here's the way to encapsulate the exitappreceiver into a basic class, and then let the other activity that needs to be implemented to close the activity to inherit it.


code Example:

Only two of the more critical codes are posted here:

1. Receiving Events

public class Baseactivity extends Activity {public static final String TAG = BaseActivity.class.getName (); public static final String broad_cast_message = TAG + ".        Broad_cast_message ";        Private Exitappreceiver mreceiver = null;                @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Registerreceiver ();          } private void Registerreceiver () {intentfilter filter = new Intentfilter ();              try {if (mreceiver! = null) {unregisterreceiver (mreceiver);          }} catch (Exception e) {e.printstacktrace ();          } mreceiver = new Exitappreceiver ();          Filter.addaction (Broad_cast_message);    Registerreceiver (mreceiver, filter); } @Override protected void OnDestroy () {if (mreceiver! = null) {Unregisterreceiver (mreceiver        );    } Super.ondestroy ();  }      Class Exitappreceiver extends Broadcastreceiver {@Override public void onreceive (context context, Int            Ent Intent) {if (Intent.getaction (). Equals (Broad_cast_message)) {finish (); }        }    }}

2. Trigger Event:

public class Twoactivity extends baseactivity {    private int getlayoutresid () {        return r.layout.activity_two;    }        @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);                Setcontentview (Getlayoutresid ());                Button Nextbutton = (button) Findviewbyid (R.id.activity_two_button);        Nextbutton.setonclicklistener (New Onclicklistener () {                        @Override public            void OnClick (View v) {                Sendbroadcast ();}}        );    }        private void Sendbroadcast () {        Intent Intent = new Intent ();        Intent.setaction (baseactivity.broad_cast_message);        Sendbroadcast (intent);    }}


Android Exit program (bottom)--use broadcast mechanism

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.