A: Getting Started with Android-about oncreate methods under activity

Source: Internet
Author: User

Original:http://www.2cto.com/kf/201112/113331.html

There are two sentences in the <intent-filter> element in the Androidmanifest.xml file:

<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>

Do you know what is the meaning of the two words in the middle? Do you know the implementation process of the Android app?

When the written app is posted to the phone, when you double-click the app's icon in the "drawer", the system will wrap the click time as a intent, which contains two parameters, the two parameters described above are passed to the app, and the intent filter that matches the intent is found in the app's feature manifest file. , if the match succeeds, find the activity element that matches the intent filter, and then look for its corresponding activity class based on the <activity> element's "name" attribute. Then the Android operating system creates an instance object of the activity class, and after the object is created, it executes to the OnCreate method of the class, which is implemented by overriding the OnCreate method of the parent's activity. The OnCreate method is used to initialize the activity instance object. The following is the code for the OnCreate method in the Helloworld.java class:

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}

The function of Super.oncreate (savedinstancestate) is to invoke the OnCreate method of its parent activity to realize the drawing work of the interface. It is important to remember to call this method when implementing the OnCreate method of your defined activity subclass to ensure that the interface is drawn.

The role of Setcontentview (R.layout.main) is to load an interface. The parameter passed in the method is "R.layout.main", which means the static constant of the static inner class layout in the R.java class, the value of main, which is a pointer to the Main.xml file under the layout subdirectory of the Res directory. This represents the display of the main.xml defined by the image.

Second onCreate (bundle savedinstancestate) parameter bundle

Original Address http://blog.sina.com.cn/s/blog_797cd06b01012ein.html

The OnCreate method is called by the system when activity is created, and is the beginning of an activity life cycle. But one thing that is easily overlooked is the parameter saveinsancestate of the OnCreate method. Because this parameter is seldom used in general program development.
The complete definition of the OnCreate method is as follows:

public void OnCreate (Bundle saveinsancestate) {
Super.oncreate (saveinsancestate);
}

As can be seen from the above code, the parameters of the OnCreate method are parameters of a bundle type. The data of the bundle type is similar to the data of the map type, which stores the data in the form of Key-value.
The saveinsancestate, literally, is the state of the saved instance. In fact, Saveinsancestate is the state that holds the activity. So where does the state data in the saveinsancestate come from? Below we introduce another method of activity onsaveinsancestate.
The Onsaveinsancestate method is used to preserve the state of the activity. This method is called to save the state when an activity is completed before the end of the life cycle. This method has a parameter name that is the same as the OnCreate method parameter name. As shown below:

public void Onsaveinsancestate (Bundle saveinsancestate) {
Super.onsaveinsancestate (saveinsancestate);
}

In practice, when an activity is completed, if it needs to be saved, in Onsaveinsancestate, the state data is placed in the form of Key-value in Saveinsancestate. Thus, when an activity is created, the state data can be obtained from the OnCreate parameter saveinsancestate.

State this parameter in the implementation of the application has a great use, such as: A game before exiting, save the current game running state, when the next time you open the last time to continue to play. Another example: E-book programs, when a novel is read to the 199th page after the exit (whether it is not enough memory or the user automatically close the program), the next time you open, the reader may have forgotten the last time you read the page, but the reader would like to continue the last reading. If the saveinstallstate parameter is used, it is easy to solve the above problem.

Simple Case API in snake game in Snakeview class

Private int[] Coordarraylisttoarray (arraylist<coordinate> Cvec) {
int count = Cvec.size ();
int[] Rawarray = new Int[count * 2];
for (int index = 0; index < count; index++) {
Coordinate c = cvec.get (index);
RAWARRAY[2 * index] = c.x;
RAWARRAY[2 * index + 1] = C.Y;
}
return rawarray;
}

Public Bundle saveState () {
Bundle map = new bundle ();
Map.putintarray ("Mapplelist", Coordarraylisttoarray (mapplelist));
Map.putint ("Mdirection", Integer.valueof (mdirection));
Map.putint ("Mnextdirection", Integer.valueof (mnextdirection));
Map.putlong ("Mmovedelay", Long.valueof (Mmovedelay));
Map.putlong ("Mscore", Long.valueof (Mscore));
Map.putintarray ("Msnaketrail", Coordarraylisttoarray (Msnaketrail));
return map;
}

Implemented in Snakeactivity

@Override

Publicvoid onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.snake_layout);
Msnakeview = (Snakeview) Findviewbyid (R.id.snake);
Msnakeview.settextview ((TextView) Findviewbyid (R.id.text));
if (savedinstancestate = = null) {
We were just launched--set up a new game
Msnakeview.setmode (Snakeview.ready);
} else {
We are being restored
Bundle map = Savedinstancestate.getbundle (Icicle_key);
if (map! = null) {
Msnakeview.restorestate (map);
} else {
Msnakeview.setmode (Snakeview.pause);
}
}
}

and rewrite Onsavedinstancestate (), this method will be called at the end of the activity.

@Override

Publicvoid onsaveinstancestate (Bundle outstate) {
Store the game state
Outstate.putbundle (Icicle_key, Msnakeview.savestate ());
}

A: Getting Started with Android-about oncreate methods under activity

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.