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