This example describes the activity usage of the four components of Android programming. Share to everyone for your reference, specific as follows:
Here is a detailed description of how to create an activity, lifecycle, memory management, startup mode.
Create an activity
I. Defining activity
1. Define activity Definition class inheritance activity
2. Declare <activity> in the node of Androidmanifest.xml
Explicit intent to create an activity three ways
The first way: constructor, code less
Intent intent1 =new Intent (this,newactivity.class);
StartActivity (intent1);
The second way: Class name form, flexible, extensible strong
Intent Intent2 =new Intent ();
Intent2.setclassname (This, "cn.test.activity.NewActivity");
StartActivity (Intent2);
The Third Way: Package name Class Name form, can start other program activity
Intent intent3 =new Intent ();
Intent3.setclassname ("Cn.test.taskdownloader", "cn.test.taskdownloader.MainActivity");
StartActivity (INTENT3);
Two. Create an activity and pass data
1. A bundle object is encapsulated in the intent object that can be used to carry data
2. The intent object can be obtained in the new activity to obtain the data stored in the bundle
Transfer data Mode one
Intent intent1 =new Intent (this,newactivity.class);
Bundle bundle=new Bundle ();
Bundle.putstring ("Data", "Test");
Intent1.putextras (bundle);
StartActivity (intent1);
Get intent Way One
Bundle bundle2 =getintent (). Getextras ();
Toast.maketext (this,bundle2.getstring ("Data"), 0). Show ();
Transfer Data Mode two
Intent intent2 =new Intent (this,newactivity.class);
Intent2.putextra ("Data", "Test2");
StartActivity (Intent2);
Get Intent Mode two
Toast.maketext (This,getintent (). Getstringextra ("Data"), 0). Show ();
Three. Create activity get Return data
1. Open activity using Startactivityforresult (Intent Intent, int requestcode) method
2. Rewrite onactivityresult (int requestcode, int resultcode, Intent data) method
3. After the call to the Setresult (int resultcode, Intent data) setting in the new activity returns data, the Onactivityresult method is invoked when the activity is closed
The requested activity:
public class Mainactivity extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
/**
* Create activity get Return Data
* @param view
*
/public void CreateNew (view view) {
Intent intent1 =new Intent (this,newactivity.class);
Intent1.putextra ("Data", "Request Date");
Open activity waits for return
Startactivityforresult (intent1,100);
}
/**
* Overrides Onactivityresult, this method is called by closing the activity *
* *
protected void Onactivityresult (int requestcode, int ResultCode, Intent data) {
Toast.maketext (this, "Requestcode: +requestcode+", Requestcode: "+resultcode+", data " +data.getstringextra ("result"), 0). Show ();
}
The activity returned:
public class Newactivity extends activity {
@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.new_activity);
Toast.maketext (This,getintent (). Getstringextra ("Data"), 0). Show ();
Sets the return data Intent to the
=new Intent ();
Data.putextra ("Result", "returned data");
Setresult (200,data);
}
Four. Implicit intent to create an activity
1. An explicit intent is to specify a component when creating intent, while implicit intent does not specify the component, and the corresponding component is matched by action, type, data
2. Define <activity> in the manifest file need to define <intent-filter> to be initiated implicitly
3. <intent-filter> configure at least one <action> and one <category>
The action, category, and data set in the 4.Intent object must all be included in <intent-filter> to start
5.<intent-filter> <action>, <category>, <data> can be configured multiple, intent objects do not have all the match, each match one can start
6. If an intent can match multiple activity,android systems will prompt the selection
Life cycle
I. Three states of Acitivity
Running: The activity runs at the front
Pause: Activity visible, but front end also has other acti vity, is covered part, or the front-end activity is transparent
Stop: Activity not visible, completely overwritten
Two. Life cycle related methods
OnCreate: Called when it is created, or when it is reopened when it is killed in a paused, stopped state.
Called after onstart:oncreate or when resuming from a stop state
Called after Onresume:onstart or from a paused state recovery, OnStart is also invoked as a result of a call to the Onresume
OnPause: Enter pause, stop state, or destroy to call
OnStop: Enter the stop state, or destroy to call
OnDestroy: Called when destroying
Onrestart: Called when resuming from stop state
As shown in the figure:
Three. How to save information
Onsaveinstancestate: Called when the activity is passively destroyed or stopped, to save the running data and to exist in the bundle
Onrestoreinstancestate: This method is invoked when the activity is redrawn, such as changing the screen orientation, savedinstancestate the data saved for Onsaveinstancestate
Memory management
When the Android system runs multiple processes, it forces an end to some processes if the system is low on resources. Priority is given to which process to choose to end. The following order depends on precedence over
Null: All activity in the process has been destroyed
Background: There is an activity in the process that stops the state
Service: A running service in the process
Visible: An activity with a paused state in the process
Foreground: An activity is running in progress
Startup mode
1. Android:launchmode properties can be configured in the <activity> tags in androidmanifest.xml to control the startup mode of Actvity
2. The acitivity we create in the Android system are presented as stacks
Standard: A new activity is created at the top of the stack every time a call to StartActivity () is started
Singletop: If the activity is started, the specified activity is not created on the top of the stack, such as at the top of the stack, no longer created
Singletask: Create if a started activity does not exist, if there is a direct jump to the specified activity location
SingleInstance: Create if a started activity does not exist, move the specified activity to the top of the stack if it exists
I hope this article will help you with the Android program.