Use broadcast mechanism to exit multiple activity in Android

Source: Internet
Author: User

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
  1. Public class Baseactivity extends Activity {
  2. protected Broadcastreceiver broadcastreceiver = new Broadcastreceiver () {
  3. @Override
  4. public void OnReceive (context context, Intent Intent) {
  5. Finish ();
  6. }
  7. };
  8. @Override
  9. public void Onresume () {
  10. Super.onresume ();
  11. //Register a broadcast in the current activity
  12. Intentfilter filter = new Intentfilter ();
  13. Filter.addaction ("Exitapp");
  14. This.registerreceiver (this.broadcastreceiver, filter);
  15. }
  16. @Override
  17. protected void OnDestroy () {
  18. //TODO auto-generated method stub
  19. Super.ondestroy ();
  20. this.unregisterreceiver (this.broadcastreceiver);
  21. }
  22. }

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
  1. Ublic class Activity1 extends Baseactivity {
  2. private Button btn1;
  3. @Override
  4. protected void OnCreate (Bundle savedinstancestate) {
  5. //TODO auto-generated method stub
  6. super.oncreate (savedinstancestate);
  7. Setcontentview (R.LAYOUT.A1);
  8. BTN1 = (Button) Findviewbyid (R.ID.BTN1);
  9. Btn1.setonclicklistener (new View.onclicklistener () {
  10. @Override
  11. public void OnClick (View v) {
  12. Intent i = new Intent (Activity1. This, Activity2.  class);
  13. StartActivity (i);
  14. }
  15. });
  16. }
  17. /** 
  18. * Capture Phone physical Menu key
  19. */
  20. private Long exittime = 0;
  21. @Override
  22. Public boolean onKeyDown (int keycode, keyevent event) {
  23. if (keycode = = keyevent.keycode_back) {//&& event.getaction () = = Keyevent.action_down
  24. if ((System.currenttimemillis ()-exittime) > ) {
  25. Toast.maketext (Getapplicationcontext (), "Press again to exit the program", Toast.length_short). Show ();
  26. Exittime = System.currenttimemillis ();
  27. } Else {
  28. Myexit ();
  29. }
  30. return true;
  31. }
  32. return Super.onkeydown (KeyCode, event);
  33. }
  34. protected void Myexit () {
  35. Intent Intent = new Intent ();
  36. Intent.setaction ("Exitapp");
  37. This.sendbroadcast (Intent);
  38. Super.finish ();
  39. }
  40. }

Entire Project Engineering:

    • Quitapp.zip (1.7 MB)
    • Download number of times: 180

Use broadcast mechanism to exit multiple activity in Android

Related Article

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.