Menu learning, and how to implement a step off all activity exit application

Source: Internet
Author: User

1 first describes how to write the menu, if you want to add the menu button in the activity, overwrite the following 2 methods in the activity:

public boolean oncreateoptionsmenu(Menu menu) and public boolean onoptionsitemselected(MenuItem item), And loads the menu's XML interface in Oncreateoptionsmenu. XML interface in the Res file under the Menu folder to create an XML file to add the menu option, you can also add an icon for the options, such as file. The onoptionsitemselected then implements the action after clicking on the corresponding entry.

<menu xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:app= "http://schemas.android.com/ Apk/res-auto "    xmlns:tools=" Http://schemas.android.com/tools "    tools:context=" Com.example.demo.MainActivity ">    <item        android:id=" @+id/close_all "        android:title=" full exit "       / ><item        android:id= "@+id/close_this"        android:title= "full current"       /></menu>

2, when the application opens a lot of activity, want to exit must press a lot of return key, we can in the menu implementation one step exit:

There are three ways of exiting:

 

Tested: 1 Although the program can be closed but does not empty the task stack, that is, the exit failed. 2 also cannot implement program exit completely. 3 requires permissions, more for the security Butler and other software, it can only close other applications, can not shut down their own applications, attention also needs permissions .

3, in order to achieve the full program exit:

(1) We can define a class app that inherits application, which is an instance of the application, as long as the program survives, defining a set member variable to be stored in all activity and overwrite the Oncreat method of the class (called only when the application first runs) to instantiate the member variable inside.

(2) After the app class is created, open the manifest file, click Application, then click on the browse preview in the back of name, it will load out just write the App class, click the select click OK to configure the app to the application in the manifest file:

(3) Then in each activity's Oncreat method, the activity is added to the collection of the app, and the activity is removed from the collection in OnDestroy (). Finally, when you click on "Full Exit" in the menu, finish off all the activity.

4, the demo is as follows: Realize the above mentioned:

1, activity1 and activity2 XML files are a TextView and button one is to show the first few activity, one is click to activate another activity, so here is not given.

2. App Category:

public class App extends application {//record all the activity's collection arraylist<activity>activities;//private// Create a global instance of the entire application,//execute @overridepublic void OnCreate () {activities=new arraylist<activity> () during the first time the entire program is created; Super.oncreate ();}}

3 mainactivity Java file: In the Activity2 is also in the Oncreat method bar activity added to the collection, and then OnDestroy remove activity, so here just give mainactivity file

public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {s        Uper.oncreate (savedinstancestate);        App app= (APP) getapplication ();        App.activities.add (this);    Setcontentview (R.layout.activity_main);    } public void Start2 (View v) {Intent intent=new Intent ();    Intent.setclassname ("Com.example.demo", "Com.example.demo.Activity2");    StartActivity (Intent); } @Override//Implement the interface of value load public boolean oncreateoptionsmenu (Menu menu) {menuinflater inflater=new menuinflater (this); Inflater.inflate (R.menu.main, menu); return Super.oncreateoptionsmenu (menu);} @Override//Implement Menue Click event: Current exit, and full exit of public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {   Case R.id.close_all:app app= (App) getapplication (), for (Activity act:app.activities) act.finish ();// Printing results show that: After the activity is finished, the activity object in App.activities is not removed, it is the activity,//of the operation task stack. So you can use the advanced for Loop System.out.println (App.activities.size ()); bReak;case r.id.close_this:finish (); break;default:break;} return super.onoptionsitemselected (item);} @Override//When the return key is pressed, the activity is destroyed so it is also removed from the collection activityprotected void OnDestroy () {app app= (APP) getapplication ();        App.activities.remove (this); Super.ondestroy ();} }

  

Menu learning, and how to implement a step off all activity exit application

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.