Android two times back key exit

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/javacattle/article/details/41964045

just in *. Java files can be added.
private int back = 0;
===============back two times exit ===================== @Override Public boolean Oncreateoptionsmenu (Menu menu) {//necessary to change menu.   Add (0, 2, 2, "exit");  return Super.oncreateoptionsmenu (menu); }
@Override public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case 2:myback ();   Break  } return super.onoptionsitemselected (item); }
@Override public boolean onKeyDown (int keycode, keyevent event) {//two times back exit if (keycode = = Keyevent.keycode_back) {    back++;     Switch (back) {case 1:toast.maketext (mainactivity.this, "Press Exit Again", Toast.length_short). Show ();    Break     Case 2:back = 0;//initialization of the back value myback ();    Break   The return true;//is set to False to invalidate the back, and true to indicate that it is not invalidated} else {return Super.onkeydown (KeyCode, event); }  }
public void Myback () {//Close program MainActivity.this.finish (); Android.os.Process.killProcess (Android.os.Process.myPid ());//Close Process}

Android two times back key exit

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.