Describes in detail how to create an Activity, lifecycle, memory management, and startup mode.
Create Activity
1. Define Activity
1. Define Activity definition class to inherit Activity
2. declare in the AndroidManifest. xml node <activity>
Three methods of explicitly creating an Activity
// Method 1: constructor with less code
Intent intent1 = new Intent (this, NewActivity. class );
StartActivity (intent1 );
// Method 2: Class Name format, flexible, and highly scalable
Intent intent2 = new Intent ();
Intent2.setClassName (this, "cn. test. activity. NewActivity ");
StartActivity (intent2 );
// Method 3: package name class name to start Activity in other programs
Intent intent3 = new Intent ();
Intent3.setClassName ("cn. test. taskdownloader", "cn. test. taskdownloader. MainActivity ");
StartActivity (intent3 );
2. Create an Activity and pass data
1. A Bundle object is encapsulated in the intent object and 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.
// Data transmission method 1
Intent intent1 = new Intent (this, NewActivity. class );
Bundle bundle = new Bundle ();
Bundle. putString ("data", "Test ");
Intent1.putExtras (bundle );
StartActivity (intent1 );
// Method 1
Bundle bundle2 = getIntent (). getExtras ();
Toast. makeText (this, bundle2.getString ("data"), 0). show ();
// Method 2 for data transmission
Intent intent2 = new Intent (this, NewActivity. class );
Intent2.putExtra ("data", "Test2 ");
StartActivity (intent2 );
// Method 2
Toast. makeText (this, getIntent (). getStringExtra ("data"), 0). show ();
3. Create an Activity to obtain the returned data
1. Use startActivityForResult (Intent intent, int requestCode) to open the Activity
2. Override the onActivityResult (int requestCode, int resultCode, Intent data) Method
3. After setResult (int resultCode, Intent data) is called in the new Activity to set the returned data, the onActivityResult method will be called when Activity is disabled.
Requested activity:
Public class MainActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
/**
* Create an Activity to obtain the returned data
* @ Param view
*/
Public void createNew (View view ){
Intent intent1 = new Intent (this, NewActivity. class );
Intent1.putExtra ("data", "request data ");
// Open Activity and wait for return
StartActivityForResult (intent1, 100 );
}
/**
* Override onActivityResult. If Activity is disabled, this method is called.
*/
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Toast. makeText (this, "requestCode:" + requestCode + ", requestCode:" + resultCode + ", data" + data. getStringExtra ("result"), 0 ). show ();
}
}
Returned activity:
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 ();
// Set the returned data
Intent data = new Intent ();
Data. putExtra ("result", "returned data ");
SetResult (200, data );
}
}
4. implicit intention to create an Activity
1. explicit intent means that a component is specified when the intent is created, while implicit Intent means that no component is specified, and corresponding components are matched through action, type, and data.
2. Define <intent-filter> when defining <activity> in the inventory file to be implicitly started
3. Configure at least one <action> and one <category> in <intent-filter>. Otherwise, the instance cannot be started.
4. The action, category, and data set in the Intent object must be included in <intent-filter> to start
5. You can configure multiple <action>, <category>, and <data> in <intent-filter>. You do not need to match all Intent objects, and each matching item can be started.
6. If one intent can match multiple activities, the Android system will prompt you to select
Lifecycle
I. Three statuses of Acitivity
Run: The activity runs at the frontend.
Paused: The activity is visible, but there are other acti labels on the front end, which are partially overwritten, or the front-end activity is transparent.
Stop: The activity is invisible and completely overwritten.
Ii. lifecycle related methods
OnCreate: called during creation, or when the program is re-opened after it is killed in the paused or stopped state.
OnStart: called after onCreate or when it is restored from the stopped state
OnResume: called after onStart or when it is restored from the pause state. When onStart is called, onResume is also called.
OnPause: it is called when it is paused, stopped, or destroyed.
OnStop: indicates that the instance is in the stopped status or called upon destruction.
OnDestroy: called upon destruction
OnRestart: called when the stop status is restored
:
Iii. Methods for saving information
OnSaveInstanceState: called when the Activity is passively destroyed or stopped. It is used to save running data and store the data in the Bundle.
OnRestoreInstanceState: This method is called when the Activity is re-drawn, for example, changing the screen direction and savedInstanceState to the data saved by onSaveInstanceState
Memory Management
When the Android system runs multiple processes, if the system resources are insufficient, some processes are forcibly terminated. It takes priority to select the process to end. In the following order, the priority is terminated
Null: all activities in the process have been destroyed.
Background: There is a stopped Activity in the process.
Service: A running Service in a process
Visible: There is a paused Activity in the process.
Foreground: An Activity is running in the process.
Startup Mode
1. You can configure the android: launchMode attribute in the <activity> label of AndroidManifest. xml to control the Startup Mode of Actvity.
2. In the Android system, the Acitivity we created is presented as a stack.
Standard: each time startActivity () is called, a new Activity is created and placed on the top of the stack.
SingleTop: if the specified Activity is not created at the top of the stack, It is not created at the top of the stack.
SingleTask: If the started Activity does not exist, it will be created. If yes, it will jump directly to the specified Activity location.
SingleInstance: If the started Activity does not exist, it is created. If yes, the specified Activity is moved to the top of the stack.
From: Fu rongkang Column