Android app exit implementation

Source: Internet
Author: User

With the rapid development of smartphones, Android plays an important role. The setting of virtual buttons greatly improves the user experience. The setting of the virtual back key also replaces the use of the physical keyboard. However, there are always advantages and disadvantages in everything. Although the design of virtual keys improves the user experience, there are also misoperations. On the first page of the application, click back, which may cause the program to exit. Here, we can take the optimizations made by common apps for this situation.

When you click the back button on the first page of the application, there are two common methods to prompt

1) Use the pop-up dialog box to display and click OK to exit. Click Cancel to exit.

2) use Toast to display: "click back again to exit the program", and click to exit the program immediately. If you click back later, it will display "re-Press the back key to exit the program"


For the first exit method:

1) rewrite the onKeyDown method and listen to the back click event.

2) set the pop-up dialog

Protected Dialog onCreateDialog (int id)

{

Return new AlertDialog. Builder (this). setTitle ("exit ")

. SetMessage ("exit? ")

. SetPositiveButton ("OK", new DialogInterface. OnClickListener ()

{

@ Override

Public void onClick (DialogInterface dialog, int which)

{

Finish ();

}

}). SetNegativeButton ("cancel", null). create ();

}

3) Implement showDialog (id); method; where id is of the int type, you can distinguish between different dialogs. Here there is only one dialog box, so this id is random.


For the second method:

Let's analyze it here: Click the back key, and the toast prompts "re-Press the back key to exit the program", and then immediately press the back key to exit, if you press the back key after a period of time, the toast will prompt you to "press the back key again to exit the program ". Therefore, we can set a flag to determine whether the user clicks the back key for the first time, and set a time-effectiveness for the mark. Only clicking the button within the time range will exit, after this time is exceeded, the toast prompt is displayed. The specific method is as follows:

1) set the identifier of a global variable: private boolean hasPressedBack;

2) rewrite the onKeyDown method and listen to the back click event.

3) set the timeliness for the global variable identifier. The Code is as follows:

Public boolean onKeyDown (int keyCode, KeyEvent event)

{

Switch (keyCode)

{

Case KeyEvent. KEYCODE_BACK:

If (! HasPressedBack)

{

// Press

HasPressedBack = true;

Toast. makeText (MainActivity. this, "repeat back to exit", Toast. LENGTH_SHORT). show ();

MHandler. postDelayed (new Runnable ()

{

@ Override

Public void run ()

{

HasPressedBack = false;

}

}, 3000); // used to set the timeliness of global variable labeling

// It will expire 3 seconds later.

Return true;

}

Break;

Default:

Break;

}

Return super. onKeyDown (keyCode, event );

}



In fact, such exit is not a real exit. It is just an illusion that it is closed and will not run again, will not occupy the phone or device memory.

In fact, this is because smartphone users have all transitioned from PCs, and they all feel that the application will not occupy resources until it is closed. Therefore, in order to cater to people's usage habits, android, this illusion. To achieve true exit, you need to turn off the process corresponding to the application in the mobile phone process. This is the true exit.

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.