Android---Click Back key two times to exit the program---Two Level interface exit program

Source: Internet
Author: User

Method One: Realize the way, by recording the key time to calculate the timing difference realization:

Import Android.app.activity;import android.os.bundle;import android.view.keyevent;import android.widget.Toast; public class Mainactivity extends Activity {    private long exittime = 0;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    }    @Override Public    boolean onKeyDown (int keycode, keyevent event) {        if (keycode = = keyevent.keycode_back) {            Exit ();            return false;        }        Return Super.onkeydown (KeyCode, event);    }    public void exit () {        if (System.currenttimemillis ()-exittime >) {            toast.maketext ( Getapplicationcontext (), "Press again to exit the program",                    Toast.length_short). Show ();            Exittime = System.currenttimemillis ();        } else {            finish ();            System.exit (0);}}    

Demo Effect:

In fact, it has been realized, but many people's intention is not in the main interface two times the return key exit program, they may enter the program is the login interface, in return, do not want to return to the login interface, skip it and directly exit the program, this time, the above code can only return to the previous interface, that is, the login interface! When we debug the program to find the finish (); System.exit (0); really let the program have the effect of quitting, but give an illusion seems to automatically restart, this involves the life cycle of the activity problem, each activity has its own life cycle, activity is separate, to handle user action, So although finish (); System.exit (0); The code executes, but only the current activity is over, but the other active activity does not end, which is what we see as if it were back to the previous interface or the software was restarted.

The above is purely personal opinion, if the wrong welcome advice.

About the activity of the detailed introduction you can own Baidu, you can also refer to this article http://www.cnblogs.com/lyp3314/archive/2011/11/10/2244971.html

2. Okay, here's a direct demonstration of how to exit the program directly on the two level interface

Import Android.app.activity;import android.os.bundle;import android.view.keyevent;import android.widget.Toast; public class Ejjm extends Activity {    private long exittime = 0;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.LAYOUT.EJJM);    }    @Override Public    boolean onKeyDown (int keycode, keyevent event) {        if (keycode = = keyevent.keycode_back) {            Exit ();            return false;        }        Return Super.onkeydown (KeyCode, event);    }    public void exit () {        if (System.currenttimemillis ()-exittime >) {            toast.maketext ( Getapplicationcontext (), "Press again to exit the program",                    Toast.length_short). Show ();            Exittime = System.currenttimemillis ();        } else {            finish ();            System.exit (0);}}    

Main function class

Import Android.app.activity;import android.os.bundle;import android.view.keyevent;import android.widget.Toast; public class Mainactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); btn= (Button) Findviewbyid (R.id.button1); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method stubintent intent=new Intent (Ma Inactivity.this,ejjm.class); MainActivity.this.startActivity (Intent), Finish ();//If you don't write finish, there will be a return effect});}    }

Activity_main.xml and Ejjm.xml inside the content I will not show, here just do a simple jump, in Activity_main.xml only put a button just

Demo effect: (desired picture)

Intent intent=new Intent (mainactivity.this,ejjm.class);

MainActivity.this.startActivity (Intent);

Finish ();//If you do not write finish, you will have a return effect, but you cannot exit the program.
The demo is as follows:

Law two: The basic principle of realization is that when the back key is pressed, it is captured by onkeydown and the back key is determined, then the exit method is executed.
In the Exit method, the value of Isexit is first judged, if False, it is set to true, and a prompt pops up, and a message is issued after 2000 milliseconds (2 seconds), and the value is reverted to false in handler. If the back key is pressed again within 2 seconds of the sending message interval, the exit method is executed again, and the exit method is executed when the value of Isexit is true. Specifically I will not write, you can refer to the following Weibo

Http://blog.sina.com.cn/s/blog_4fd2a65a0101gg2o.html

Android---Click Back key two times to exit the program---Two Level interface exit program

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.