Handling of horizontal and vertical screen switching for Android

Source: Internet
Author: User

In Android, by default, the current activity is destroyed when the screen is switched and the activity is restarted. Of course, this default processing method may not meet our requirements. There are multiple options to change this processing method.

 

One way is to save the data before destroying the activity and load the configuration when the activity creates again. Onsaveinstancestate stores data before the activity is destroyed.

 

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

if (dataString != null) {
outState.putString("data", dataString);
}
}

Oncreate (bundle savedinstancestate) is responsible for data recovery

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataString = savedInstanceState.getString("data");
}

 

Onretainnonconfigurationinstance () and getlastnonconfigurationinstance () can implement similar functions. The former is used to save data, and the latter is used to restore data, but only when the activity is restored immediately after it is destroyed.

 

A more common method is to prevent activity destruction and restart. You must first add an attribute to the corresponding activity in androidmanifest. xml.

android:configChanges="keyboardHidden|orientation"

Then, the onconfigurationchanged (configuration newconfig) function is intercepted in the program and processed by itself. In this case, the activity will not be restarted during screen switching. If necessary, you can reconfigure the Layout Based on the screen direction.

public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setupLandScapeViews();
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setupPortraitViews();
}

}

If you do not want to change the display mode of the screen, for example, if you want to maintain a display mode in the game, you can add

android:screenOrientation="portrait"

In this way, the screen orientation will not change the display mode or restart the activity.

 

 

Now we will discuss the differences between the two storage methods:

When the program is running, oncreate () is then run to onresume. Once the screen is switched to onpause (), ondestroy (), then the program will re-oncreate () and then re-onresume (). Ondestroy () is executed during screen switching ().

Save the screen conversion and Activity Status
Compare the usage of onsaveinstancestate () and onretainnonconfigurationinstance () in different requirements
For the Android platform, no matter what the purpose is, it is more or less necessary to jump to multiple activities, including to obtain some system resources and necessary information, by starting (startactivity () & startactivityforresult () child activity is used to provide a selector or as a medium for user input information. During this period, the parent activity temporarily loses focus, so that some necessary information is temporarily stored through the onsaveinstancestate () method before this. When the parent activity becomes the current focus again, the system will trigger onrestoreinstancestate () to restore the original data before losing focus! Onretainnonconfigurationinstance () also has the same purpose to process similar requests. It mainly changes the display mode due to the rotating device, and then triggers the call of this method. In case of certain requirements, especially when the display mode changes after the device is rotated, what conditions should be used to determine which method can be used to better meet the needs? Before making a choice, it is necessary to understand the characteristics of the two methods.

Onsaveinstancestate () after other activities are started through the new intent in the current activity, it will automatically save its own data through this method. When it returns again, it can use onrestoreinstancestate () restore data. This method will also be called when the screen display mode changes after the device is rotated. Note that the entire process is completely controlled by the system and a custom data cannot be returned through onsaveinstancestate. In addition, onsaveinstancestate () is called in the destroying process of all activities. It is only used to re-create an activity with the same status as the previous one after returning to the specific activity. For example, when we start some connections, the State cannot still save the connection status.
Therefore, when onsaveinstancestate is called, all current connections will be destroyed together. When onrestoreinstancestate () is used for the second time to retrieve the previous connection settings and create a new connection entity. If you have more discoveries or do not use the above verification results, thank you very much for participating in the discussion on this topic. Onretainnonconfigurationinstance () when the device configuration changes, destroying is called by the system. This method can retain the previous activity state like the onsaveinstancestate () method. The biggest difference is that this method can return an object containing State information, it can even contain the activity instance itself. The newly created activity can inherit a large amount of parent activity state information. After the activity state is saved using this method, the original state is restored in the new activity instance through getlastnonconfigurationinstance.

 

The biggest benefit of this method is:
* When an activity obtains image or video information through a network resource,
You can quickly load the status information of the entire activity without getting the original resource address again.
* When the activity contains many threads, the original thread can still be held after the change, without the need to re-create the process to restore the original state.
* When the activity contains some connection instances, the connection can also be maintained throughout the change process.
The following are some notes:
* Onretainnonconfigurationinstance () is called after onsaveinstancestate.
* The call sequence is also between onstop () and ondestroy.
Next, we will use an example to briefly understand the functions of onretainnonconfigurationinstance () and getlastnonconfigurationinstance ().
Usage. In this example, an activity with two buttons is started. One of the buttons is used to call the local address book,
And pass the selected item as the return value to the current activity. Another button is used to view the selected communication information.
The normal process is that after the program is started for the first time, the second button for viewing information is unavailable. When the pick button is selected
When the address book content is displayed, the status of the "view information" button is changed to "operable. Then, when the configuration of the device is changed,
You can note that even if the activity is re-built through oncreate (), the previously guaranteed UI attributes remain in the last operation state.

In order to save image resources and network connections, it is better to use onretainnonconfigurationinstance () even if it is used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.