Android Exit Program (iii)--android event bus

Source: Internet
Author: User
Tags eventbus

Overview

When we move forward with the footsteps of time, we always find ourselves doing some foolish things before. But it's not always a bad thing. I wrote two ways in my previous blog about how to quit an application in Android. Recently, I found a seemingly tall on the method, because as a programmer of you, it will be more convenient to use. It's like a handy tool for you when you need it, and it feels good to have it. I'll use the Android event bus below to make a description of the Android exit program.


Androideventbus Introduction

Androideventbus is an Android platform event bus framework that simplifies interaction between components such as activity, Fragment, service, and so on, greatly reducing the coupling between them, making our code more concise and less coupled, Improves the quality of our code. But what it can do is not limited to these.


Instructions for use

Requirements of the JAR package:

Android-support-v4.jar

Simple_eventbus.jar

Note: The above two jar package is included in the following project source download connection, you can go directly to the download. (PS: When you really start using them, you'll find them so good)


Key Code

Baseactivity.java

public class Baseactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Eventbus.getdefault (). Register (baseactivity.this);    }        @Override    protected void OnDestroy () {        Super.ondestroy ();        Eventbus.getdefault (). Unregister (baseactivity.this);    }        @Subcriber (tag = "Exit_app")    private void Finishapp (String msg) {        finish ();    }}

Startactivity.java

public class StartActivity extends baseactivity {    private int getlayoutresid () {        return r.layout.activity_main;    }        Private Button Mgonextbutton = null;        @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);                Setcontentview (Getlayoutresid ());                Initevent ();    }        private void Initevent () {        initviews ();                Setviews ();    }        private void Initviews () {        Mgonextbutton = (Button) Findviewbyid (R.id.activity_main_go_sample_actviity_a_button );    }        private void Setviews () {        Mgonextbutton.setonclicklistener (new Onclicklistener () {                        @Override            public void OnClick (View arg0) {                startactivity (new Intent (Startactivity.this, Sampleaactivity.class));            }        });    }}

Exitactivity.java

public class    Exitactivity extends baseactivity {private int getlayoutresid () {return r.layout.activity_exit;        } private Button Mgonextbutton = null;                @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (Getlayoutresid ());    Initevent ();                } private void Initevent () {initviews ();    Setviews ();    } private void Initviews () {Mgonextbutton = (Button) Findviewbyid (R.id.activity_exit_finish_button);                        } private void Setviews () {Mgonextbutton.setonclicklistener (new Onclicklistener () {            @Override public void OnClick (View arg0) {Exitapp ();    }        });    } public void Exitapp () {Eventbus.getdefault (). Post ("", "Exit_app"); }}


Project Source

http://download.csdn.net/detail/u013761665/8624649


Reference Connectionhow can I play with the Android event bus?

Related Posts

Android Exit program (one)--singleton mode


Android Exit Program (ii)--using the broadcasting mechanism

Android Exit Program (iii)--android event bus

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.