I. Getting started with Android-about the onCreate method under Activity, androidoncreate

Source: Internet
Author: User

I. Getting started with Android-about the onCreate method under Activity, androidoncreate

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

The <intent-filter> element in the AndroidManifest. xml file contains the following two sentences:

 

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

 

Do you know what the two sentences mean? Do you know the execution process of the Android app?

After a written application is published to a mobile phone, When you double-click the icon of the application in the "drawer", the system packs the click time into an Intent, which contains two parameters, after the two parameters mentioned above are passed to the application, find the intent filter that matches the intent in the application's function list file. If the match succeeds, find the Activity element of the matched intent filter, and find its corresponding activity Class Based on the "name" attribute of the <Activity> element. Then, the Android operating system creates an instance object for this Activity class. After the object is created, the onCreate method of this class is executed. This onCreate method is implemented by rewriting the onCreate method of the parent Activity class. The onCreate method is used to initialize the Activity instance object. The code for the onCreate method in the helloWorld. java class is as follows:

 

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}

 

The role of super. onCreate (savedInstanceState) is to call the onCreate method of its parent Activity to draw pictures on the interface. When implementing the onCreate method of the Activity subclass defined by yourself, remember to call this method to ensure that the interface can be drawn.

SetContentView (R. layout. main) is used to load an interface. The input parameter in this method is "R. layout. main ", which means R. in the java class, the value of the static constant main of the static internal class layout is changed to the main under the layout subdirectory under the res directory. the identifier of the xml file. Therefore, the screen defined in main. xml is displayed.

 

Article 2 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 the Activity is created. It is the beginning of the Activity lifecycle. However, it is easy to ignore that the parameter saveInsanceState of the onCreate method. This parameter is rarely used in general program development.
The complete onCreate method is defined as follows:

Public void onCreate (Bundle saveInsanceState ){
Super. onCreate (saveInsanceState );
}

From the code above, we can see that the onCreate method parameter is a Bundle type parameter. Bundle-type data is similar to Map-type data, which stores data in the form of key-value.
Literally, saveInsanceState stores the instance status. In fact, saveInsanceState stores the Activity state. So where does the State data in saveInsanceState come from? Next we will introduce another method of Activity saveInsanceState.
The onsaveInsanceState method is used to save the Activity state. This method is called to save an Activity before its lifecycle ends. The parameter name of this method is the same as that of the onCreate method. As follows:

Public void onSaveInsanceState (Bundle saveInsanceState ){
Super. onSaveInsanceState (saveInsanceState );
}

In practice, if you need to save the status before an Activity ends, put the status data in the form of key-value in onsaveInsanceState. In this way, when an Activity is created, the status data can be obtained from the onCreate parameter saveInsanceState.

The status parameter has a great purpose in implementing the application. For example, a game can save the running status of the current game before exiting, and continue playing the game the next time it is enabled. Another example is the e-book program. When a novel is read to 199th pages, it exits (whether it is insufficient memory or the user automatically closes the program). When it is opened next time, the reader may have forgotten the page on which he had read the last time. However, the reader wants to continue reading the last time. If the saveInstallState parameter is used, the preceding problem can be easily solved.

In the simple example api, the snake game is in the snakkeview 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;
}

Implement

@ Override

Publicvoid onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. Snail ke_layout );
MSnakeView = (snail keview) 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 (snail keview. READY );
} Else {
// We are being restored
Bundle map = savedInstanceState. getBundle (ICICLE_KEY );
If (map! = Null ){
MSnakeView. restoreState (map );
} Else {
MSnakeView. setMode (snail keview. PAUSE );
}
}
}

 

And override onSavedInstanceState (). This method will be called at the end of activity.

@ Override

Publicvoid onSaveInstanceState (Bundle outState ){
// Store the game state
OutState. putBundle (ICICLE_KEY, mSnakeView. saveState ());
}

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.