Example analysis of activity usage of four components of Android programming
This article describes the activity usage of the four components of Android programming. Share with you for your reference, as follows:
How to create activity, life cycle, memory management and startup mode are described in detail here.
Create activity
1、 Define activity
1. Define activity class to inherit activity
2. Declare < activity > in the node of androidmanifest.xml
There are three ways to explicitly create an activity
2、 Create an activity and pass data
1. A bundle object is encapsulated in the intention object, which can be used to carry data
2. In the new activity, you can obtain the intention object to obtain the data saved by the bundle
3、 Create an activity to get the return data
1. Use the startactivityforresult (intent, int requestcode) method to open the activity
2. Override the onactivityresult (int requestcode, int resultcode, intent data) method
3. in the new Activity, setResult (int resultCode, Intent data) is set to return data. After closing the Activity, the onActivityResult method is called.
Requested activity:
Returned activity:
4、 Implicitly create an activity
1. Explicit intention refers to that the component is specified when creating the intention, while implicit intention does not specify the component. The corresponding component is matched through action, type and data
2. When defining < activity > in the manifest file, you need to define < intent Filter > to be started implicitly
3. Configure at least one < action > and one < category > in < intent Filter >, otherwise it cannot be started
4. The action, category and data set in the intent object must be included in < intent Filter > to start
5. < action >, < category >, < data > in < intent Filter > can be configured with multiple. There is no need to match all the intent objects, and each object can be started by matching one
6. If an intention can match multiple activities, the Android system will prompt you to select
life cycle
1、 Acitivity three states
Run: the activity runs at the front end
Pause: the activity is visible, but there are other activities on the front end, which are partially covered, or the front end activity is transparent
Stop: the activity is invisible and completely overwritten
2、 Life cycle related methods
Oncreate: called when creating, or when the program is restarted after being killed in the suspended or stopped state
OnStart: called after oncreate or when recovering from a stopped state
Onresume: called after OnStart or when resuming from the suspended state. When resuming from the stopped state, onresume will also be called because OnStart is called
Onpause: called when entering the pause, stop, or destroy state
Onstop: called when entering the stop state or destroying
Ondestroy: called when destroying
Onrestart: called when recovering from a stopped state
As shown in the figure:
3、 Methods of saving information
Onsaveinstancestate: called when the activity is destroyed or stopped passively. It is used to save the running data. The data can be stored in the bundle
Onrestoreinstancestate: this method is called when the activity is redrawn, such as changing the screen direction, and savedinstancestate is the data saved by onsaveinstancestate
memory management
When the Android system runs multiple processes, if the system resources are insufficient, some processes will be forced to end. Priority is given to which process to end. The top priority ends in the following order
Empty: all activities in the process have been destroyed
Background: there is an activity in a stopped state in the process
Service: a running service in a process
Visible: there is a suspended activity in the process
Foreground: an activity is running in the process
Startup mode
1. Android: launchmode attribute can be configured in the < activity > tag in androidmanifest.xml to control the startup mode of actvity
2. In the Android system, the acitivity we created is presented in the form of stack
Standard: each time startactivity () is called to start, a new activity will be created and placed at the top of the stack
Singletop: if the specified activity is not at the top of the stack, it will be created. If it is at the top of the stack, it will not be created
Singletask: if the started activity does not exist, it will be created. If it does exist, it will directly jump to the location of the specified activity
Singleinstance: if the started activity does not exist, it will be created. If it does exist, it will move the specified activity to the top of the stack
I hope this article will help you in Android programming.