It is recommended to learn about the content of Activity Lifecycle in the official Android tutorial. The website address is https://developer.android.com...
Solution:
Put the initialization operation in the OnCreate() 方法中,而不是 OnResume() 或者 OnStart() method of Activity.
You can also set the static global variable identifier to 0, set it to 1 after initialization, and determine whether the variable is 0 before initialization. However, this is not the right way. It is recommended to understand LifeCycle to solve this problem.
The first phenomenon is that the data has been initialized again, so the solution is as follows: 1. Think about why the data has been initialized again. As mentioned above, maybe you initialized it in the OnResume(), OnStart() methods 2. Generate The cause of the problem is that the data is initialized multiple times, so you can add the function of multiple calls to the data initialization. Use object-oriented ideas to encapsulate the initialization data, which is a little bit like the single-interest model. The advantage of this is to solve the problem that may occur due to multiple executions. question,
Every time you enter the page, determine whether the data already exists. If it already exists, use cache and do not request the network to load the data.
Usually, onCreate will be called again when the Activity is destroyed. Check the startup mode of the current Activity and the startup mode of another Activity.
Put the initialization data operation in the onCreate method. You may put it in onResume.
It is recommended to learn about the content of Activity Lifecycle in the official Android tutorial.
The website address is https://developer.android.com...
Solution:
Put the initialization operation in the
OnCreate()
方法中,而不是OnResume()
或者OnStart()
method of Activity.You can also set the static global variable identifier to 0, set it to 1 after initialization, and determine whether the variable is 0 before initialization. However, this is not the right way. It is recommended to understand LifeCycle to solve this problem.
Attached is a picture of Lifecycle
The first phenomenon is that the data has been initialized again, so the solution is as follows:
1. Think about why the data has been initialized again. As mentioned above, maybe you initialized it in the OnResume(), OnStart() methods
2. Generate The cause of the problem is that the data is initialized multiple times, so you can add the function of multiple calls to the data initialization. Use object-oriented ideas to encapsulate the initialization data, which is a little bit like the single-interest model. The advantage of this is to solve the problem that may occur due to multiple executions. question,
If every time your
Activity
都destroy
了话,那么你把初始化的程序放到onCreate
中自然达不到你想要的结果。所以你还是先打下log
看下你Activity
lifecycle.Every time you enter the page, determine whether the data already exists. If it already exists, use cache and do not request the network to load the data.
By logging and combining it with the android life cycle function, the problem is basically solved
It means your activity has been killed, consider the reason
Usually, onCreate will be called again when the Activity is destroyed. Check the startup mode of the current Activity and the startup mode of another Activity.
You can consider the startup mode of your Activity, there are 4 startup modes, there is always one suitable for you
onSavedInstance()
onRestoreInstance()