Android Development Mainactivity Class

Source: Internet
Author: User

First, mainactivity:

1, each language has a program storage (such as: C#main function), and the Android program's entry is the Mani actiivty function.

2, activity is the core class of Android (Android.app.Activity), in activity class has OnCreate event method, generally used to initialize the activity, And by placing the view on the activity with the Setcontentview method, the activity displays the controls on the view after binding.

               mainacitvity.java files mainactivity:
      & nbsp                          ,         &NB Sp    a, OnCreate () is the entry function to create the form,
                      &NBS P                        b, Oncreateoptionsmenu () is the dish that creates the current activity Single function,
                                &N Bsp              c, onoptionsitemselected () Is the Response menu button click event Function.
                                               d, Placeolderfragment inner class is the implementation of the custom fragment


The ①oncreate function is used to initialize the activity instance object, and in the previous activity blog we know that the OnCreate method is called by the system when the activity is created.
Is the beginning of an activity life cycle.

protected void onCreate (Bundle savedinstancestate) {superonCreate (savedinstancestate); Setcontentview ( R.layout.activity_main);}

1. Bundle parameter type: Store data in the form of Key-value.
2, in fact, Saveinsancestate is to save the state of activity, saveinsancestate state data is from the onsaveinsancestate.

 Public void onsaveinsancestate (Bundle saveinsancestate) {super. Onsaveinsancestate (saveinsancestate);}

3, when an activity is completed, if you need to save the state, in the Onsaveinsancestate, the state data in the form of Key-value into the saveinsancestate. This way, when an activity is created,
Can get state data from OnCreate parameter Saveinsancestate, Onsaveinsancestate method
is used to preserve the state of the activity.
4. When an activity ends in the life cycle, this method has a parameter name that is the same as the OnCreate method parameter name. (For example: a game before exiting, save the current game running state, when the next time you open the last time to continue to play.) ), in Onsaveinsancestate, the state data is placed in the form of Key-value into Saveinsancestate. Thus, when an activity is created, the state data can be obtained from the OnCreate parameter saveinsancestate.

5, rewrite onsavedinstancestate (), this method will be called at the end of activity.
Override right-Source-->override in the Mainactivity class.

②oncreateoptionsmenu () menu function

@Override Public BooleanOncreateoptionsmenu (Menu menu) {/*** This method is used to initialize menus where the menu parameter is the menu instance that is about to be displayed.         Returns true to show that the menu,false is not displayed; * (only called when the menu is initialized for the first time) inflate the menus;         This adds items to the Action Bar * if it is present. */getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public BooleanOnprepareoptionsmenu (Menu menu) {/*** After the oncreateoptionsmenu is executed, the menu is called before it is displayed, and if the menu is already created, it is called before the menu is displayed. Similarly, * Returns true to show that the menu,false is not displayed; (This method can dynamically change the state of a menu, such as loading different menus, etc.) TODO * auto-generated method stub*/        return Super. Onprepareoptionsmenu (menu); } @Override Public voidonoptionsmenuclosed (Menu menu) {/*** Called every time the menu is closed. (The menu is closed in three scenarios, the Menus button is clicked again, the Back button is clicked, or the user selects a menu item) TODO * Auto-generated Method stub */        Super. onoptionsmenuclosed (menu); } @Override Public Booleanonoptionsitemselected (MenuItem item) {/*** The menu item is invoked when clicked, that is, the method of listening for the food item. * With these methods, you can be informed that for activity, only one menu object can be displayed and monitored at the same time. TODO auto-generated * Method Stub*/        return Super. onoptionsitemselected (item); }

③onoptionsitemselected () Determines whether there is a change menu, there is a return of true, and finally calls the Placeolderfragment internal class fragment life cycle related state method.

   Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }

Android Development Mainactivity Class

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.