当前位置:网站首页>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 :
-
onCreate: establish .
-
onStart: Visible and non interactive , I.e. not at the front desk .
-
onResume: Visible and interactive , That is, it is already at the front desk .
-
onPause: Visible but not interactive .
-
onStop: stop it , Invisible, non interactive
-
onDestroy: The destruction .
-
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
边栏推荐
- 杂七杂八的学习
- BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题
- C connection of new world Internet of things cloud platform (simple understanding version)
- winform滚动条美化
- Fill the network gap
- [recommendation of new books in 2021] enterprise application development with C 9 and NET 5
- 记录webView显示空白的又一坑
- AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
- 常用UI控件简写名
- Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
猜你喜欢
[2021 book recommendation] practical node red programming
Ffmpeg common commands
【2021年新书推荐】Practical Node-RED Programming
机器学习笔记 一:学习思路
Record WebView shows another empty pit
Encapsulate a set of project network request framework from 0
【2021年新书推荐】Red Hat RHCSA 8 Cert Guide: EX200
Component based learning (1) idea and Implementation
ViewPager2实现画廊效果执行notifyDataSetChanged后PageTransformer显示异常 界面变形问题
Component learning (2) arouter principle learning
随机推荐
Markdown basic grammar notes
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
[recommendation for new books in 2021] professional azure SQL managed database administration
从0开始封装一套项目的网络请求框架
DCMTK(DCM4CHE)与DICOOGLE协同工作
C#新大陆物联网云平台的连接(简易理解版)
Project, how to package
【2021年新书推荐】Artificial Intelligence for IoT Cookbook
MarkDown基础语法笔记
电脑关机程序
oracle给对象重命名
JVM basics you should know
去掉状态栏
Ffmpeg common commands
iTOP4412内核反复重启
MySQL笔记4_主键自增长(auto_increment)
Explore how @ modelandview can forward data and pages through the source code
MySQL notes 4_ Primary key auto_increment
adb shell top 命令详解
PaddleOCR 图片文字提取