Google Baidu a bit, Android out of multiple activity methods, we discussed a lot.
In the internship, see the company's project exits a number of activities, is the use of LinkedList method, graduation design time, also refer to that method. At the time of completion, you can use the broadcast mechanism to exit activity when you do not see it online. Read some of the People's blog, articles and other tutorials, found also excerpt "very casually", said not detailed, or can not be achieved.
Look at their meaning, write the demo, we look at it. The main code is as follows: (inconvenient to see directly under the whole project)
For the sake of simplicity of the code, extract a base class baseactivity (custom, of course, you can also not write this base class, as long as you implement the code in each activity of your project), Let the activity that you want to close in your code inherit from this baseactivity.
Java code
- Public class Baseactivity extends Activity {
- protected Broadcastreceiver broadcastreceiver = new Broadcastreceiver () {
- @Override
- public void OnReceive (context context, Intent Intent) {
- Finish ();
- }
- };
- @Override
- public void Onresume () {
- Super.onresume ();
- //Register a broadcast in the current activity
- Intentfilter filter = new Intentfilter ();
- Filter.addaction ("Exitapp");
- This.registerreceiver (this.broadcastreceiver, filter);
- }
- @Override
- protected void OnDestroy () {
- //TODO auto-generated method stub
- Super.ondestroy ();
- this.unregisterreceiver (this.broadcastreceiver);
- }
- }
Add the Myexit () method to the activity you want to close, and then call the Myexit () method where you want to exit the program operation.
Java code
- Ublic class Activity1 extends Baseactivity {
- private Button btn1;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- //TODO auto-generated method stub
- super.oncreate (savedinstancestate);
- Setcontentview (R.LAYOUT.A1);
- BTN1 = (Button) Findviewbyid (R.ID.BTN1);
- Btn1.setonclicklistener (new View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- Intent i = new Intent (Activity1. This, Activity2. class);
- StartActivity (i);
- }
- });
- }
- /**
- * Capture Phone physical Menu key
- */
- private Long exittime = 0;
- @Override
- Public boolean onKeyDown (int keycode, keyevent event) {
- if (keycode = = keyevent.keycode_back) {//&& event.getaction () = = Keyevent.action_down
- if ((System.currenttimemillis ()-exittime) > ) {
- Toast.maketext (Getapplicationcontext (), "Press again to exit the program", Toast.length_short). Show ();
- Exittime = System.currenttimemillis ();
- } Else {
- Myexit ();
- }
- return true;
- }
- return Super.onkeydown (KeyCode, event);
- }
- protected void Myexit () {
- Intent Intent = new Intent ();
- Intent.setaction ("Exitapp");
- This.sendbroadcast (Intent);
- Super.finish ();
- }
- }
Entire Project Engineering:
- Quitapp.zip (1.7 MB)
- Download number of times: 180
Use broadcast mechanism to exit multiple activity in Android