当前位置:网站首页>Reading notes - activity

Reading notes - activity

2022-04-23 07:16:00 Lance_ w

Activity Reading notes

First of all Activity Life cycle of , Brief review :

  1. onCreate: establish .

  2. onStart: Visible and non interactive , I.e. not at the front desk .

  3. onResume: Visible and interactive , That is, it is already at the front desk .

  4. onPause: Visible but not interactive .

  5. onStop: stop it , Invisible, non interactive

  6. onDestroy: The destruction .

  7. onReStart: Restart . That is, the execution is finished onPause and onStop But not implemented onDestroy, Execute when restarted .

Two questions about the life cycle :

distinguish
onStart onStop Visible and invisible
onResume onPause Interactive and non interactive
from Activity 1 start-up Activity 2 The process of

First of all 1 Do not interact with users , perform onPause after ,1 Visible but not interactive , Then it will be executed 2 The creation of , namely onCreate、onStart、onResume, When 2 It can be seen that after interaction , Corresponding 1 No longer at the front desk , Can no longer interact , namely 1 Of onStop.

So to make sure Activity Efficiency of startup , Can no longer Activity Of onPause、onStorp Perform time-consuming actions in .


Activity The life cycle of the exception

Activity Exception destroy , Data storage :onSaveInstanceState、onReStoreInstanceState
problem :onSaveInstanceState Under what circumstances is executed ???
onSaveInstanceState The call for is in onStop It was certain before , But with onPause The timing relationship between them is not necessarily , May be in onPause Before , Or maybe onPause after .

1、 First, in the Activity in ,onSaveInstanceState The execution code is as follows ,onSaveInstanceState stay performSaveInstanceState Be performed in :

final void performSaveInstanceState(Bundle outState) {
    
      onSaveInstanceState(outState);
      saveManagedDialogs(outState);
      mActivityTransitionState.saveState(outState);
      storeHasCurrentPermissionRequest(outState);
      if (DEBUG_LIFECYCLE) Slog.v(TAG, "onSaveInstanceState " + this + ": " + outState);
  }

2、performSaveInstanceState The call is passed ActivityThread Class callCallActivityOnSaveInstanceState Method is executed to , In the middle step, ignore .callCallActivityOnSaveInstanceState The code snippet is as follows :

private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
    
      r.state = new Bundle();
      r.state.setAllowFds(false);
      if (r.isPersistable()) {
    
          r.persistentState = new PersistableBundle();
          mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
                  r.persistentState);
      } else {
    
          mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
      }
  }

3、 stay ActivityThread in callCallActivityOnSaveInstanceState Called from multiple places , Here we mainly look at two places :performPauseActivity and performStopActivity. Here are two internal callbacks Activity Corresponding onPause and onStop Method , Of course, some other operations are included ,onSaveInstanceState It's contained in it .

Let's take a look at performPauseActivity,performStopActivity It's similar inside :

if (!r.activity.mFinished && saveState) {
    
        callCallActivityOnSaveInstanceState(r);
    }
    //Activity Of onPause Method is called inside the method 
    performPauseActivityIfNeeded(r, reason);
    //performStopActivityIfNeeded(r, reason);

It can be seen here that onPause It was possible to perform callCallActivityOnSaveInstanceState Methodical , namely onSaveInstanceState Method ,callCallActivityOnSaveInstanceState The execution of the method needs to meet two conditions , One is flag—mFinished, Have you executed finish Method , So that explains why onBackPressed And execution finish The method is normally closed Activity Not execute onSaveInstanceState Method ; Another condition is flag—saveState( Just look at the name , Indicates whether data needs to be stored ).

4、onPause、onStop In the implementation of callCallActivityOnSaveInstanceState The main difference is saveState, stay performPauseActivity in ,saveState The final value depends on the function ActivityClientRecord Of isPreHoneycomb Method , This method judges the current Applicatio Of targetSdkVersion Whether the target compiled version is greater than Android3.0, namely Android3.0 Before version onSaveInstanceState Execution in onPause Before . and performStopActivity Medium saveState Then the value passed in directly is true, That's execution onStop Before if Activity unexecuted finish Method words ,onSaveInstanceState Method will be called , That is, the status of exception destruction .

public boolean isPreHoneycomb() {
    
        if (activity != null) {
    
            return activity.getApplicationInfo().targetSdkVersion
                  < android.os.Build.VERSION_CODES.HONEYCOMB;
        }
        return false;
    }

onReStoreInstanceState It is in Activity Called when the exception destroy is created again , Data is saved and restored through Bundle Carrier complete ,onReStoreInstanceState Methods and onCreate Methods will carry onSaveInstanceState Data saved when calling , but onReStoreInstanceState Called bundle Will not be empty ,onCreate Under normal circumstances bundle by null, So in onCreate To recover data in, you need to check and verify the data .

For some special configuration changes , Don't want to Activity Recreate the problem , for example sim Card change 、 Horizontal and vertical screen switching 、 Language switching and so on , Can pass menifest life activity Time configuration configChanges Tag properties to prevent activity restart , common sim Card change (mcc、mnc); Horizontal and vertical screen changes (orientation)、 Language change (locale) etc. , The corresponding can be through congestion activity Of onConfigurationChanged Method to monitor this line config Change some operations that need to be handled .

Activity Start mode of

1、standard: The standard model , Each start Activity Will create a new one Activity example . A new start Activity Will be added to the original Activity In the stack of , So pass ApplicationContext start-up standard Of Activity Will report a mistake , Tips Activity need ACTIVITY_NEW_TASK flag, because ApplicationContext No task stack .

2、singleTop: Top of stack singleton mode . It needs to be activated Activity Stack top , There is no need to recreate , Otherwise recreate .

3、singleTask: In stack singleton mode . As long as the present Activity There is a stack that needs to be started Activity, Will not recreate , Otherwise recreate .

4、singleInstance: Full singleton mode . On a single task stack .

TaskAffinity: Task relevance .

TaskAffinity And singleTask Of Activity Use a combination of , You can specify Activity Task stack .

for example : There is Application 1,Activity A1 Has been launched ,ActivityB1 Belong to App1,ActivityB1 For the above TaskAffinity It specifies App1 Package name and singleTask Of Activity

When App2 Inside Activity A2 start-up App1 ActivityB1 when , Normal as ActivityB1 And App2 Activity A2 For the same stack , but ActivityB1 The stack is specified as App1 Package name , At this time will be App2 home Key and then enter , The discovery is not ActivityB1, And open App1, It is found that the top of the stack is ActivityB1.

application : When two App Belonging to a subordinate relationship ,App1 start-up ,App2 When other applications start , We hope App2 Depend on App1, Instead of a stand-alone stack , You can set App2 Correlation Activity Of TaskAffinity by App1 The package name .

TaskAffinityh and allowTaskReparenting( Allow redirection of parent )

under these circumstances ,Activity It can be moved from the task stack it starts to the task stack associated with it ( If the task stack appears in the foreground ).

contrast :TaskAffinity And singleTask Of Activity Use a combination of ,TaskAffinity The task stack already exists , Will be Activity Move to the stack corresponding to the setting , If it doesn't exist, it still stays on the current stack .

and TaskAffinityh and allowTaskReparenting, It's when Activity When the set stack appears in the foreground , You can take what already exists Activity Move back to the set stack .

版权声明
本文为[Lance_ w]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230608223752.html