huazi

huazi

"Notes on 'Android Development Art Exploration' - Chapter 1"

1. Comprehensive Analysis of Activity Lifecycle#

Normal lifecycle

  • onCreate: Indicates that the Activity is being created.
  • onReStart: Indicates that the Activity is being restarted. Generally, when the current Activity transitions from an invisible state to a visible state, onReStart is called. If the current Activity resumes after pausing onPause and onStop, this method will be called.
  • onStart: Indicates that the Activity is being started. At this point, the Activity is already visible, but it has not yet appeared in the foreground and is not interactive. It can be understood that the Activity has been displayed, but it is not visible yet.
  • onResume: Indicates that the Activity is already visible and active in the foreground. Both onStart and onResume indicate visibility, but onStart means that the Activity is still in the background, while onResume means that the Activity is displayed in the foreground.
  • onPause: Indicates that the Activity is being stopped. At this point, you can perform operations such as storing data and stopping animations, but they should not be time-consuming, as this will affect the display of the Activity. oPause must be executed first before the onResume of the new Activity is executed.
  • onStop: Indicates that the Activity is about to stop. You can perform heavyweight recycling operations, but they should not be time-consuming either.
  • onDestroy: Indicates that the Activity is about to be destroyed. This is the last callback in the Activity lifecycle and can be used for cleanup and resource release.

If the current Activity opens a new Activity using a transparent theme, the onStop callback will not be triggered.

onStart and onStop are callbacks based on visibility, while onResume and onPause are callbacks based on whether the Activity is in the foreground. In practice, there is not much difference between them.

Before a new Activity is started, the top Activity in the stack needs to call onPause first before the new Activity can start.

Both onPause and onStop should not perform time-consuming operations, especially onPause. Therefore, it is recommended to perform operations in onStop.

Normally, when the system configuration changes, the Activity will be destroyed. When the Activity is terminated under exceptional circumstances, the system will call onSaveInstanceState to save the state of the current Activity. The callback timing is before onStop. When the Activity is rebuilt, the system will call onRestoreInstanceState, which is called after onStart in terms of timing.

Each View has onSaveInstanceState and onRestoreInstanceState methods, and by looking at their implementation, you can know which data they can restore for the view.

The system only calls onSaveInstanceState and onRestoreInstanceState to store and restore data when an abnormal termination occurs, and not in other cases.

2. Activity Launch Modes#

  • standard: Standard mode, each time an Activity is launched, a new instance is created and pushed onto the stack. By default, it enters the task stack of the Activity that launched it.
  • singleTop: Single top mode, if the new Activity is already at the top of the stack, the Activity will not be recreated and the onNewIntent method will be called.
  • singleTask: Single task mode, as long as the Activity exists in a stack, multiple launches of this Activity will not create a new instance and the onNewIntent method will be called. If it does not exist, the system will first look for the required task stack. If the task stack does not exist, it will create a task stack and push it onto the stack. If the task stack exists, it will check if there is an instance in the stack. If there is, it will move it to the top of the stack; if not, it will create a new instance and push it onto the stack.
  • singleInstance: Single instance mode, similar to singleTask, but with the addition that Activities in this mode can only exist in a separate task stack. Due to the nature of single task mode, no new Activities will be created subsequently.

The task stack name can be configured through TaskAffinity, and by default, the task stack name is the package name.

Task stack A is the foreground task stack, and task stack B is the background. When the back key is pressed, task stack A is popped first, then task stack B is popped, and finally it returns to the home screen.

Question: What launch mode should be used when sharing to a third-party application?

The FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS flag indicates that this Activity will not appear in the list of recent Activities, equivalent to android="true".

3. Matching Rules of IntentFilter#

Activity launch can be explicit or implicit. Explicit calls require specifying the information of the target object, including the class name and package name. Implicit calls do not require this.

IntentFilter filters information based on action, category, and data.

  • action: The action in the Intent must match exactly with the action in the filter rule, including case sensitivity.
  • category: When startActivity is called, the system will automatically add the android.intent.category.DEAFAULT category, so the intent can be without a category, but if it exists, it must match one of them.
  • data: If the filter rule defines data, the Intent must also define matching data; data consists of two parts, mimeType and URI. If the URI is not specified, the default values are content and file (schema).
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.