Why is onCreate called in Activity when the Android tablet is spinning?

The onCreate method is called every time in the Activity every time the screen is rotated. Is it a click on the CreateCreate button again or is the whole activity created again?

+4
source share
2 answers

It is not just onCreate (). When the screen is rotated, the action pauses, stops and restarts. See this question for more information: Activity Life Cycle - onCreate is called upon every reorientation

If the question: "Why is this happening?" The answer is related to functionality inside Android apps and windows. More specifically, there is currently no way for Android to move, resize, and relay each view when changing orientation. To make it possible to use this scenario, a simpler implementation of simply tearing down activity and backing it up in a different orientation was implemented.

+8
source

When the orientation is changed, the onDistroy method is called, which indicates that the activity is closed, and again a new activity is created with a new height and width.

when the orientation is changed, all objects of the Activity class will be destroyed, and when the Activity is restarted, they will be created again, if there is a large amount of data, it will take more time to reload all the data .. therefore, he prefers to separate and save all the data in the Non-Activity class and use in the Activity class by creating objects in the NonActivity class.

when the orientation change to the SaveInstanceState method and
using onSaveInstanceState, data will set or save some values ​​when activity destroys and recreates

protected void onSaveInstanceState(Bundle icicle) { super.onSaveInstanceState(icicle); icicle.putLong("param", value); } 

when the activity restarts the create method call again, and this time the Bundle returns the value you saved in onSaveInstanceState

 public void onCreate(Bundle icicle) { if (icicle != null){ value = icicle.getLong("param"); } } 
+2
source

Source: https://habr.com/ru/post/1441426/


All Articles