Beginner Android Development essay Menu, toast Usage, activity of the four startup mode and a convenient base activity class usage method

Source: Internet
Author: User

Toast

Toasts are a great way to remind your Android system

Start by defining a trigger point for a popup toast, such as a button

Where Toast.length_short refers to the length of the display and a built-in variable for Toast.length_long can be selected.

Toast.maketext (firstactivity. This, "You clicked Button 1", Toast.length_short). Show ();
Menu

Start by creating a new menu folder in the Res directory to create a main.xml code similar to the following

<Menuxmlns:android= "Http://schemas.android.com/apk/res/android">    <ItemAndroid:id= "@+id/add_item"Android:title= "Settings"/>    <ItemAndroid:id= "@+id/remove_item"Android:title= "Help"/></Menu>

Where item is the specific menu item ID for the identifier title is the name

Then override the Oncreateoptionsmenu () method in the activity (shortcut Ctrl+o)

@Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; inflate Accept both parameters the first is the resource file The second one is added to which menu object. The return value determines if it can be displayed!!! 
Define Menu Response event override onoptionsitemselected () method @Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseR.id.add_item:toast.maketext ( This, "You click Settings", Toast.length_short). Show (); Break; CaseR.id.remove_item:toast.maketext ( This, "You click Help", Toast.length_short). Show (); Break; default: } return true; }
Four startup modes of activity

Standard, Singletop, singletask and singleinstance four kinds of

The modification method is to specify Android:launchmode for the corresponding activity tag in androidmanifest.xml.

Standard is the default active startup mode (add a bit of Android with a return stack management activity) that either is sufficient and exists on the return stack, or the start creates an instance of the activity

The singletop corresponds to standard mode, and it is used directly if the activity that exists in the return stack no longer creates a new activity instance. ( using it can be a good solution to the problem of repeatedly creating the top of the stack!!!) )

Singletask can make an activity exist only one instance in the context of the entire application. When you discover and directly use an existing instance activity, all the activities on top of that activity are all out of the stack!!!

SingleInstance Special + Complex

The activity in this mode launches a new return stack to manage the activity in order to facilitate the sharing of activity instances. such as explaining the convenience of other programs to access the activity secondactivity

How to use base activity classes

Simplify the code by writing an active public parent class

 Public classBaseactivityextendsappcompatactivity {Private Static FinalString TAG = "Baseactivity"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); LOG.D (TAG, GetClass (). Getsimplename ());
activitycollector.addactivity ( This);
Because the Activity Manager uses a list for staging activities, it is possible to use addactivity () to add the activity currently being created to the activity manager
Then you only need to call the Removeactivity () method in the OnDestroy method }protected void Ondestory () {Super.ondestroy (); Activitycollector.removeactivity ( this); //So where do you want to exit the program just call Activitycollector.finishall ()? }}

Ps

The code that kills the current process can be added after all activities have been destroyed

Android.os.Process.killProcess (Android.os.Process.myPid ());

  KillProcess () can kill a process to accept a process ID parameter, and can only be used to kill the current process, cannot kill other processes.

Beginner Android Development essay Menu, toast Usage, activity of the four startup mode and a convenient base activity class usage 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.