Android Study Notes (November 3,): screen Switching

Source: Internet
Author: User
Pay attention to data storage and recovery during switchover

In the Android Study Notes (): During the switchover of the portrait screen, we have configured two layout methods, one for normal portrait and the other for landsapce. If there is only one layout, we will use the previous example to delete the xml file in layout-land/. Then, when switching the screen, the new screen will be adapted according to the original layout. I simplified the program. Every time I press pick, I add one to track whether data needs to be saved and restored, as shown below:

public class Chapter19Test3 extends Activity{private Button pickButton = null;private int count = 0;        protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setupViews();}private void setupViews(){setContentView(R.layout.chapter_19_test1);pickButton = (Button)findViewById(R.id.c19_pick);pickButton.setText("Pick " + count);pickButton.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {count ++;pickButton.setText("Pick " + count);}});}}

This is a simple example. We found that the value was re-assigned during screen conversion. That is, it also goes through the destroy and create stages, and also needs to be saved and restored. We click the Pick button four times and use the simulator Ctrl + F12 to switch, and count is returned to 0. There are two other examples on the right. The widget is an input box. We can see that if we modify the value in the input, it will not change after rotation, because the widget content will be processed by the system, we need to deal with things other than widgets. If we track onCreate and onDestroy In the example, we will find that destroy is called every time the screen is turned, and then create. In addition, destroy-create-destroy-create is used for each switchover, but I am not sure whether it is just a simulator or not on the actual target device. We can see that there is a significant UI change after the switchover, it can explain why you re-create twice.

For more information about how to save and restore data, see Study Notes for Android (): switching between portrait and landscape screens.


No screen Switching

For some reasons, such as the game, the game fails due to slow movements during screen switching. We hope that screen switching will not be triggered. We set the activity in AndroidManifest. xml:

<Activity android: name = ". xxxxx "......Android: screenOrientation = "portrait"/>

Even if we add the relevant layout xml in layout-land/, the layout conversion between the landscape and landscape is not triggered.

It should be noted that we track and find that switching will also lead to destroy-> re-create, that is, the data needs to be saved and restored. There are many types of Android hardware devices, some of which use smartkey, some are opened through a physical keyboard, and the simulator uses CTRL + F12 to force screen switching. If we need a location gyroscope like the iPhone to trigger screen switching, we only need to <activity android: name = ". xxxxx" in AndroidManifest. xml"
......Android: screenOrientation = "sensor"/>.

Self-control rotation

If we want to control the screen selection by ourselves, the program will control the processing after receiving the related screen rotation event, rather than automatically processing it by the system. Follow these steps:

1. Add the android: configChanges attribute to the activity in AndroidManifest. xml, which means we need to control the processing by ourselves.

<activity android:name=".xxx" ... android:configChanges="keyboardHidden|orientation"  />

One strange thing is that if we only set orientation or keyboardHidden, there may be some strange phenomena, which may only occur in the simulator, because CTRL + F12 is the keyboard operation of the simulator. Not sure whether the actual device will be like this. The switch of the simulator triggers the key and orientation events. This is the default situation.

2. obtain and process related events through onConfigurationChanged () in the program.

During the switchover, the system includes events through onConfigurationChanged (). The system will not process the interface on its own, that is, it will not perform destroy and re-create operations on the activity because of the re-image, that isMost importantly, it does not cause data re-initialization, resulting in data storage and recovery., Which is also a convenient processing method.

public void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);setupViews();}

In general, newConfig can be used to sort out the interrupted status, for exampleSwitch (NewConfig. Orientation)To determine the current direction and how to deal with it. In this example, you can simply re-paint the UI.


Related links:
My Android development articles

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.