Activity casually,
Activity Lifecycle
1. Normal Lifecycle
Activity lifecycle switching process
OnStart and onResume, onPause, and onStop are different. onStart and onStop are called back and forth from the perspective of whether the Activity is visible. onResume and onPause are called back and forth from the perspective of whether the Activity is on the foreground, in addition to this difference, there is no obvious difference in actual use.
2. lifecycle analysis in case of exceptions
Resource-related system configuration changes cause Activity to be killed and re-created
Activity reconstruction process in case of exceptions
When the mobile phone rotates the screen, the system configures an edge. By default, the Activity is destroyed and re-created. When the system configuration changes, the Activity will be destroyed, its onPause, onStop, and onDestory will all be called, and the Activity will be terminated due to exceptions, the system will call onSaveInstanceState to save the status of the current Activity (normally, the system will not call this method ). This method is called before onStop and has no time sequence relationship with onPause (it may be later than or before onPause ). After the Activity is re-created, the system will call onRestoreInstanceState and pass the bundle stored in onSaveInstanceState when the Activity is destroyed to onRestoreInstanceState and onCreate methods. You can determine whether the Activity is rebuilt by checking whether the bundle is null. If it is rebuilt, the previously stored data can be retrieved and restored. In terms of time sequence, the onRestoreInstanceState is called after onStart. In general, the Activity behind the screen rotation will be re-created. However, when the configChanges attribute of the Activity is set to orientation | screenSize, the Activity will not be re-created.
3. LaunchMode of Activity
4. Flags of the Activity
FLAG_ACTIVITY_NEW_TASK
This flag is used to specify the "singleTask" Startup Mode for the Activity. The effect is the same as that specified in XML.
FLAG_ACTIVITY_SINGLE_TOP
This flag is used to specify the "singleTop" Startup Mode for the Activity. The effect is the same as that specified in XML.
FLAG_ACTIVITY_CLEAR_TOP
When an Activity with this flag is started, all the activities on the same job stack must go out of the stack.
5. matching rules of IntentFilter
Implicit call requires that Intent be able to match the filtering information set in IntentFilter of the target component. If not, the target Activity cannot be started. The filtering information in IntentFilter includes action, category, and data. An Intent matches only the action category, category, and data category at the same time. In addition, an Activity can contain multiple intent-filters. If an Intent matches any set of intent-filters, the corresponding Activity can be started successfully.
1. action matching rules
Action is a string. The system predefines some actions. You can also define your own actions in the application. Action matching requires that the action in Intent exist and must be the same as one of the actions in the filter rule. Here, you must note that it is different from the category matching rule. Action is case sensitive. actions with the same case and case strings fail to match.
2. matching rules of category
If category exists in Intent, all category must be the same as one of the filtering rules. Of course, the Intent does not have category, but it can still match successfully because the system adds "android" to the Intent by default when calling startActivity and startActivityForResult. intent. category. DEFAULT. Meanwhile, in order for our activity to receive implicit calls, you must specify the category "android. intent. category. DEFAULT" in intent-filter.
3. data matching rules
Data is composed of two parts: mimeType and URI. MimeType refers to the media type, such as image/jpeg, audio/mpeg4-generic and video/*, and URI contains more data. The URI structure is as follows:
<Scheme>: //
Scheme: URI mode, such as http, file, and content. If scheme is not specified in the URI, the entire URI is invalid.
Host: URI Host name, for example, www.biadu.com
Port: Port number in URI
Path, pathPattern, and pathPrefix: these three parameters indicate the path information. Path indicates the complete path. pathPattern also indicates the complete Path information, but it can contain wildcards; pathPrefix indicates the prefix of the path.
Finally, when we start an Activity in implicit mode, we can make a judgment to see if any Activity can match our implicit Intent. There are two judgment methods: The resolveActivity method of PackageManager or the resolveActivity method of Intent. If no matching Activity is found, null is returned.
Refer to "Android development art exploration"