SoFunction
Updated on 2025-03-11

Android Development Solution to Rebuild Activity by Rotating Screen

There is a section specifically explaining this problem in the Android development document. Simply put, Activity is the main mechanism responsible for interacting with users. Any changes in "Configuration" may affect the Activity interface. At this time, the system will destroy and rebuild the Activity to reflect the new Configuration.
"Screen Direction" is a Configuration. By looking at the javadoc of the Configuration class, you can see what other configurations there are: such as fontScale, keyboardHidden, locale, etc.
When the screen rotates, this Configuration changes, so the currently displayed Activity needs to be rebuilt, the Activity object will be terminated, its onPause(), onStop() and onDestroy() methods are fired in turn, and then a new Activity object is created and the onCreate() method is fired. Suppose that before the screen rotates, the user is filling out a registration form on his phone. If the processing is not done properly, the user will find that the rotated form has become blank, which seriously affects the user experience.
There are three ways to solve this problem:
Method 1: Disable the screen rotation
There is no doubt that this is the lazy method, which is equivalent to avoiding the questions raised in this article. Just look at the following methods:
Copy the codeThe code is as follows:

<activity android:name=".MyActivity"
android:screenOrientation="portrait"
android:label="@string/app_name">

Method 2: Restore the scene after rotation
Since the activity will be destroyed, we can use the "persistence/recovery site" method introduced earlier to solve the problem. That is, save the content currently entered by the user in the database or Preference in onPause(), and read and populate it into the form in the onCreate() method. This is also the officially recommended method.
It should be added that if Activity reconstruction requires a lot of resources or requires access to the network, it can be implemented to implement the onRetainNonConfigurationInstance() method to save the required data to an object first, like the following:
Copy the codeThe code is as follows:

@Override
public Object onRetainNonConfigurationInstance() {
final MyDataObject data = collectMyLoadedData();
return data;
}

During reconstruction, the data saved before is obtained through the getLastNonConfigurationInstance() method in the onCreate() method, as shown below:
Copy the codeThe code is as follows:

@Override
public void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {// means onCreate() not triggered by Configuration changes
data = loadMyData();
}
...
}

Method 3: Handle rotation
Generally, the configuration changes will cause the activity to be destroyed and rebuilt, but there is also a way to prevent the activity from being rebuilt when the specified Configuration changes. The method is to specify the Configuration name that needs to be ignored through the android:configChanges attribute, for example:
Copy the codeThe code is as follows:

<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">

After this setting, the Activity object will not be destroyed when the screen rotates - instead, the Activity's onConfigurationChanged() method is triggered, where the developer can obtain the current screen direction in order to make necessary updates. Since the Activity in this case will not be destroyed, the information displayed in the Activity after rotation (such as text in the text box) will not be lost.
If you use the same layout resource file for horizontal and vertical screens in your application, you can even do nothing in onConfigurationChanged(). However, if the horizontal screen uses different layout resource files from the vertical screen, such as res/layout-land/ in the horizontal screen and res/layout-port/ in the vertical screen, the setContentView() method must be called again in onConfigurationChanged() so that the new layout can take effect. At this time, although the Activity object is not destroyed, various controls on the interface have been destroyed and rebuilt. You need to write additional code to restore the interface information.
Copy the codeThe code is as follows:

@Override
public void onConfigurationChanged(Configuration newConfig) {
(newConfig);

// Checks the orientation of the screen
if ( == Configuration.ORIENTATION_LANDSCAPE) {
(this, "Sidual Screen Mode", Toast.LENGTH_SHORT).show();
} else if ( == Configuration.ORIENTATION_PORTRAIT){
(this, "portrait mode", Toast.LENGTH_SHORT).show();
}
}

OfficialAndroid development documentationThis method is not recommended to handle Configuration changes:
Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.
Best Practices
Considering that rotating the screen is not the only factor that causes the Activity to be destroyed and rebuilt, the method introduced in the previous article is still recommended: persisting the Activity state in onPause() and restoring the scene in onCreate() can kill multiple goals at one stroke. Although Google does not recommend setting the Android:configChanges attribute, if your Activity shares the same layout file horizontally and vertically, Method 3 is undoubtedly the most convenient way.