Android Learning Note (37): Talk about screen switching again

Source: Internet
Author: User

Data saving and recovery should be noted for switching

In the Android learning Note (36): Horizontal screen vertical screen switching, we configured two layout, a user common portrait, a user Landsapce way. If there is only one layout, we use the previous example, delete the XML file in layout-land/, then when the screen is switched, the original layout will be adapted to the new screen. The program I simplified, each press pick, add one, use this to track the need for data preservation and recovery, as follows:

[Java]View Plaincopy
  1. Public class Chapter19test3 extends activity{
  2. private Button Pickbutton = null;
  3. private int count = 0;
  4. protected void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setupviews ();
  7. }
  8. private void Setupviews () {
  9. Setcontentview (R.LAYOUT.CHAPTER_19_TEST1);
  10. Pickbutton = (Button) Findviewbyid (R.id.c19_pick);
  11. Pickbutton.settext ("Pick" + count);
  12. Pickbutton.setonclicklistener (new View.onclicklistener () {
  13. public void OnClick (View v) {
  14. Count + +;
  15. Pickbutton.settext ("Pick" + count);
  16. }
  17. });
  18. }
  19. }

This is a very simple example. We found that it was re-assigned when we turned the screen. That is also the phase of destroy and create, and also the need to save and restore. We clicked on the Pick button 4 times and switched through the simulator CTRL+F12, Count returned to 0. On the right is another two examples, the widget is an input box, we can see if we modify the value in the input condition, after rotation is not changed, because the content of the widget, the system will help us to deal with, and outside the widget we need to deal with. If we trace OnCreate and OnDestroy in the example, we will find that every time we turn the screen, we call destroy and create. And every time I switch, destroy-create-destroy-create, but I'm not sure if it's just the simulator, and whether it's the same on the actual target device, you can see a change in the obvious UI after switching to explain why you re-create two times.

For information on how to save and restore, see Android Learning Note (36): Horizontal screen vertical screen switch.

Do not switch on screen

For some reason, such as the game, in the process of switching the screen, due to slow motion causes the game to fail, we do not want to trigger screen switching. We set the activity in Androidmanifest.xml:

<activity android:name= ". xxxxx" ... android:screenorientation= "Portrait" />

Even if we add the relevant layout XML to layout-land/, the layout of the horizontal and vertical screens is not triggered.

It is important to note that we follow the discovery that switching can also lead to destroy->re-create, which is the need for data preservation and recovery. Because of the many types of hardware devices in Android, some through SmartKey, some through the physical keyboard open, and in the simulator is "CTRL+F12" to force the screen switch.   If we need to have a positioning gyroscope with the iphone to trigger a screen switch, we just <activity android:name= ". xxxxx" in Androidmanifest.xml ... android:screenorientation= "sensor" /> can be.

Self-control rotation

If we want to control the screen selection, after receiving the relevant screen rotation event, the program will control the processing, rather than the system automatically processing. You can use the following steps:

The activity in Androidmanifest.xml, increasing the Android:configchanges attribute, indicates that we need to control these processes on our own

[HTML]View Plaincopy
    1. <activity android:name=". xxx" ... android:configchanges="keyboardhidden|orientation" />

A little bit more peculiar is that if we only set orientation or Keyboardhidden, there will be some strange phenomenon, may only appear in the simulator, because CTRL+F12 is the emulator's keyboard operation. Unsure whether the actual device will be so. The simulator switchover triggered the key and orientation two events, which we now default to.

Second, in the program through the onconfigurationchanged () to obtain the relevant events and processing.

When switching, the system, through the onconfigurationchanged () includes the event, the system will not handle the interface itself, that is, not because of the re-depiction of the activity to destroy and re-create, that is, the most important, This is a relatively convenient way to do this without causing data to be reinitialized and to save and restore the data.

[Java]View Plaincopy
    1. Public void onconfigurationchanged (Configuration newconfig) {
    2. super.onconfigurationchanged (newconfig);
    3. Setupviews ();
    4. }

In general and, we can use the newconfig to break the former state, for example, with switch (newconfig. Orientation) to determine the current direction, and judge how to handle, in this case, Simply redrawing the UI will do.

RELATED Links: My Android development related articles

Android Learning Note (37): Talk about screen switching again

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.