Android Double-click the return button to exit Activity method

Source: Internet
Author: User

The first: the use of thread-delay implementation:

private int mbackkeypressedtimes = 0;

@Override public void onbackpressed () {if (Mbackkeypressedtimes = = 0) {Toas T.maketext (This, "Press once again to exit the program  ", Toast.length_short). Show ();                         mbackkeypressedtimes = 1;               & nbsp        new Thread () {                 & nbsp;               @Override                                  public void Run ( ) {                                         Try {              &nbs p;                                  thread.sleep (&NB);Sp                            & nbsp;         } catch (Interruptedexception e) {         &NB Sp                            & nbsp;         e.printstacktrace ();            & nbsp                          } finally {                           & nbsp                    mbackkeypressedtimes = 0;  & nbsp;                          & nbsp;      &nbsp  }                          & nbsp;     }                    and nbsp;   }.start ();                        return;                      else{                          & nbsp;    This.activity.finish ();                           }               }                super.onbackpressed ();         }



Second: The use of the Computation time difference implementation (personally think this way is relatively simple, and not prone to anomalies, the code is more secure)

Private long exittime = 0;

public void Exitapp ()
{
if ((System.currenttimemillis ()-Exittime) > 2000)
{
Toast.maketext (This.activity, "Press again to exit the program", Toast.length_short). Show ();
Exittime = System.currenttimemillis ();
} else
{
This.activity.finish ();
}

}

The third method of

  1. /**
  2. * menu, Return key response
  3. */
  4. @Override
  5. Public boolean onKeyDown (int keycode, keyevent event) {
  6. //TODO auto-generated method stub
  7. if (keycode = = Keyevent.keycode_back)
  8. {
  9. Exitby2click (); //Call the double Exit function
  10. }
  11. return false;
  12. }
  13. /**
  14. * Double Exit function
  15. */
  16. Private static Boolean Isexit = false;
  17. Private void Exitby2click () {
  18. Timer texit = null;
  19. if (Isexit = = false) {
  20. Isexit = true; //Ready to exit
  21. Toast.maketext (This, "Press again to exit the program", Toast.length_short). Show ();
  22. Texit = new Timer ();
  23. Texit.schedule (new TimerTask () {
  24. @Override
  25. public Void Run () {
  26. Isexit = false; //Cancel exit
  27. }
  28. }, 2000); //If the return key is not pressed within 2 seconds, the start timer cancels out the task just performed
  29. } Else {
  30. Finish ();
  31. System.exit (0);
  32. }
  33. }

Android Double-click the return button to exit Activity method

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.