1.Activity Introduction:
Activity is one of the most important and common components of Android apps. It provides a screen that users can interact with in order to complete a task, such as dialing, taking pictures, and so on. Each activity corresponds to a window on which the user interface can be drawn. Windows are usually filled with screens, but can also be smaller than the screen and float on top of other windows. (You can see the API documentation)
Use of 2.Activity
2.1 Creating an Activity
Inherit the activity class, overriding the OnCreate () method.
Setcontentview (int layoutresid); To add a view control to an activity, view is the interface we actually see.
1 Packagecom.example.administrator.demoactivity;2 3 ImportAndroid.os.Bundle;4 Importandroid.support.v7.app.ActionBarActivity;5 6 Public classMainactivityextendsActivity {7 @Override8 protected voidonCreate (Bundle savedinstancestate) {9 Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); One } A}
2.2 Configuring activity
Configuring Activity in Androidmanifest.xml
1 <activity2 android:name= ". Mainactivity "3 android:label=" @string/app_name ">4 <intent-filter>5 <action android:name= "Android.intent.action.MAIN"/>6 <category android:name= " Android.intent.category.LAUNCHER "/>7 </intent-filter>8 </activity >
2.3 Activating and shutting down activity
An Android app typically contains multiple activity, but only one activity acts as a portal to the program, and when the Android app starts, he automatically starts and executes the activity, Activity initiated by the entry activity or initiated by the entry activity
Activity initiates other activity usually in two ways:
(1). StartActivity (Intent Intent): Start other activity
(2). Startactivityforresult (Intent intent,int requestcode): Initiates activity with the specified request code by overriding Onactivityresult () method to get the results returned by the newly started activity
Requestcode represents the request code that initiates activity, which is specified by the developer and is primarily used to identify the source of the request. The activated activity needs to call Setresult (int resultcode,intent Intent); ResultCode is the result ID, Intent carries the returned value
Intent is a very important way to communicate between Android components, an activity that expresses its "intentions" by specifying intent.
Android offers the following two ways to turn off activity:
(1). Finish (): End Current Activity
(2). finishactivity (int requestcode): ends with Startactivityforresult according to Requestcode (Intent intent,int requestcode) Activated activity
Here is an example of using StartActivity (Intent intent,int Requestcode); method to start getting the data returned by otheractivity
First activity:mainactivity and layout layouts
1 Packagecom.example.administrator.demoactivity;2 3 Importandroid.content.Intent;4 ImportAndroid.os.Bundle;5 Importandroid.support.v7.app.ActionBarActivity;6 ImportAndroid.widget.TextView;7 8 Public classMainactivityextendsactionbaractivity {9 //identify the request sourceTen Private Static Final intFlag_get_message = 1; One PrivateTextView TextView; A @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); - -TextView =(TextView) Findviewbyid (R.id.textview); -Intent Intent =NewIntent ( This, Otheractivity.class); + Startactivityforresult (intent,flag_get_message); - } + A @Override at protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { - //Super.onactivityresult (Requestcode, ResultCode, data); - if(Requestcode = = Flag_get_message && ResultCode = =otheractivity.flag_back_message) { - if(Data! =NULL){ -String msg = Data.getstringextra ("msg")); -Textview.settext ("Information obtained from otheractivity:" +msg); in } - } to } +}View Code
1<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"2xmlns:tools= "Http://schemas.android.com/tools" android:layout_width= "Match_parent"3android:layout_height= "Match_parent" android:paddingleft= "@dimen/activity_horizontal_margin"4android:paddingright= "@dimen/activity_horizontal_margin"5android:paddingtop= "@dimen/activity_vertical_margin"6android:paddingbottom= "@dimen/activity_vertical_margin" tools:context= ". Mainactivity ">7 8<TextView9Android:id= "@+id/textview"TenAndroid:layout_width= "Wrap_content" Oneandroid:layout_height= "Wrap_content"/> A -</RelativeLayout>
View Code
Second activity:otheractivity and layout layouts
1 Packagecom.example.administrator.demoactivity;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 7 /**8 * Created by Administrator on 2015/2/21.9 */Ten Public classOtheractivityextendsactivity{ One Public Static Final intFlag_back_message = 1; A - @Override - protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_other); - - InitData (); + } - + Public voidInitData () { AIntent Intent =NewIntent ( This, Mainactivity.class); atIntent.putextra ("msg", "Otheractivity"); -Otheractivity. This. Setresult (Flag_back_message,intent);//The return identity must be set -Otheractivity. This. Finish ();//End Current Activity - } - - in}View Code
1 <?xml version= "1.0" encoding= "Utf-8"?>2 <linearlayout xmlns:android= "/http Schemas.android.com/apk/res/android "3 android:orientation=" vertical "android:layout_width=" Match_ Parent "4 android:layout_height=" Match_parent ">5 </LinearLayout>
View Code
The results show:
2.4 Using bundles to exchange data between activity
Intent provides multiple overloaded methods to carry additional data:
(1). Putextras (Bundle data):
(2). Bundle Getextras ();
(3). PutExtra (String key,xxx value); To intent in the form of key-value to add data, in fact, this way is also Putextras (bundle data), to view the source, the bundle is null is the first common one bundle and then save the data into the bundle. Here are some of the source code for the intent class:
1 Public Intent PutExtra (string name, String value) {2 if NULL {3 new Bundle (); 4 }5 mextras.putstring (name, value); 6 return This ; 7 }
(4). Getxxxextra (String name): Removes the specified data by key
Bundle is a simple data carrying packet, view source can find it contains several methods to deposit data
Putxxx (String key,xxx data): puts data of type int, long, etc. into the bundle
Putserializable (String key,serializable value); Adds a serializable object to the bundle.
3.Activity life cycle and load mode
Life cycle demonstration of 3.1 activity
The following are the official activity lifecycles provided by Android
OnCreate (): The first time the activity is created is called back, the method will only be invoked once
OnStart (): callback when initiating activity
Onrestart (): Callback when new activity is started
Onresume (): Callback when user can get focus
OnPause (): Callback When other activity is started, pause the activity
OnStop (): The activity is in an invisible state when an activity is started and is recalled
Ondestory (): callback when destroying activity
When the user presses the back key, if the previous activity is not visible, the Android system callbacks with Onrestart ()-->onstart () ...
When the user presses the back key, if the previous activity is visible, the Android system callbacks the Onresume () method;
It can be seen that when the system memory is not enough, the system will kill the activity in the OnPause () and OnStop () states
4 modes of loading for 3.2 activity
You can specify the Android:launchmode property when configuring activity, which is used to configure the load mode of the activity, which supports the properties in 4
*standard: Standard mode, default load mode
*singletop:task Top Single case mode
*singletask:task in single-case mode
*singleinstance: Global Singleton mode
Why should I specify load mode for activity?
Let's start with Android's management of activity: Android uses task to manage multiple activity, and when we start an activity, the system creates a task and then launches the activity's entry.
Android does not provide an API to the task, only by invoking the activity's GetTaskID () method to get the ID of the task it is in, we can interpret the task as the activity stack and the task to manage the activity on the stack.
The following details accept the activity's 4 load mode:
*standard Load Mode:
Each time the activity is started in this mode, Android will always create a new instance of the activity that is launched and add that activity to the current task stack, which does not create a new task, but adds the new
Activity is added to the original task
*singletop mode
This pattern is similar to standard mode, except that when the activity that is started is already at the top of the task stack, Android does not create activity instances, but takes existing activity.
*singletask mode
The activated activity has only one instance of activity within the same task, and is divided into the following three scenarios:
<1> If the target activity that is started does not exist in the task stack, the system will create a target activity instance and add it to the top of the task stack
<2>: If the target activity that you started already exists at the top of the task stack, the mode and singletop mode are the same
<3>: If the target activity that is started already exists but is not at the top of the task stack, the system will remove all activity above the target activity to the task stack, so that activity is placed on top of the task stack
*singleinstance mode
In this load mode, a target activity instance is created, regardless of which task is started, and a new task stack is used to mount the activity instance. It can be divided into two kinds of situations:
<1> If you create a target activity that does not exist, the system creates a completely new task, creates an activity instance, and then adds the target activity to the top of the new task stack.
<2> If the created target activity already exists, the system will place the activity's stack in the foreground, regardless of the task stack.
Note: Activity with singleinstance load mode is always at the top of the task stack, and the activity's task stack contains only that activity
Activity of Android four components