The life cycle of the activity of Android development and the resource adaptation scheme when the screen is switched

Source: Internet
Author: User


background : There have been two blog posts written about activity before

The life cycle of Android activity: Detailed description of the activity's entire life cycle, transitions between states, and the state of the activity that was saved when returning to the desktop

Trigger timing of Onsaveinstancestate and Onrestoreinstancestate in Android activity: the triggering time and effect of these two methods in activity are introduced.

This blog will be involved in the content, if you have some understanding can be directly down to see, if you do not know can go in memory.



problem : In the application exit dialog, found that if the dialog box pops up and then switch the phone to the screen, the dialog disappears. Knowing that activity will go through the life cycle when switching between the screen and the other, it is mistaken that the dialog box was not restored at the time of OnPause and Onresume.

The first thought is to set a global variable isdialogshow used to save the dialog box is displayed, in the OnPause check if the display, the Isdialogshow set to True, and then in Onresume to determine the value of Isdialogshow, If true, the dialog box is displayed.

Think this will solve the problem, the operation found the wrong, the dialog box is still not displayed.

Then debug found that every time you enter Onresume, Isdialogshow are false, that is, each time isdialogshow is reinitialized.

It suddenly dawned on me that the life cycle might not be what I thought when I switched to the screen.

Let's take this opportunity to summarize how the activity is saved as well as the adaptive layout when switching between the screens.



Solution Solutions : There are three options for this problem

1?? Adapt to the activity life cycle, save and then restore the state, OnCreate dynamic adaptation of the layout.

2?? Set in androidmanifest.xml to keep the activity alive to save state and then onconfigurationchanged Adaptive layout

3?? Disables the activity's portrait-to-landscape conversion, and the status and layout remain intact.



programme I :

Print out each method of activity first.

Start the app (this is the vertical screen):


Switch to horizontal screen:


The number inside is added a global variable (initialized to 0, each entry onpause automatically plus 1), with a view to the value of the change, visible in the OnPause or 1, and so on when the switch is finished back to 0.

It is also easy to understand that activity is equivalent to completely re-initializing the entire life cycle.

It is also noted that when switching to horizontal screen is more onsaveinstancestate and onrestoreinstancestate method, the above in the second blog said, the activity in there is "destroyed may ", the onsaveinstancestate method is called, giving you a chance to save the data in the activity, and then the activity is actually" destroyed without your permission "after the screen switch, so the system will call The Onrestoreinstancestate method allows you to restore the previously saved data.

So it's clear why my dialog box doesn't come out after toggling the screen, and the value of the isdialogshow used to record it is reset, and so is the solution.

Save the Isdialogshow value in the onsaveinstancestate method, and then retrieve the Isdialogshow value in the onrestoreinstancestate method. To determine if the dialog box should be displayed.

So how to adapt to the layout, such as XML resources, as long as the two sets can be, horizontal screen resources in the res/layout-land inside, vertical screen resources in the Res/layout-port folder, resource names are the same, the system will find themselves.

You can also obtain the current screen status by the following methods to implement some of the resources to load.

if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape) {//do something} else if (This.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait) {//do something}


This completes the preservation of State and adaptive resources within the full life cycle of the activity.




Scenario Two :

When talking about program two, if you can avoid the activity to re-walk the life cycle, why also design this screen switch between the time to complete re-walk the life cycle?

Since the mobile phone switch the horizontal screen, such as from the vertical screen switch to the screen, then the original vertical screen layout,drawable and other resources may not be able to adapt to the horizontal screen mode, well-designed applications will generally have two sets of resources to adapt to the vertical screen mode, so after switching to restart activity, During the new creation process, you will be

Dynamic adaptation is achieved by using the resources of the horizontal screen.

Scenario two only needs to add the following code under the Activity tab in the manifest file

android:configchanges= "Orientation"

The meaning of this code is to explain to the system, the activity of the orientation change, I do not need you to self-adaptation, I will do in the Onconfigurationchanged method of processing, I myself completed the adjustment of the vertical screen switch, That tells the activity not to go back to the life cycle and put me on.

After adding this code declaration, re-running will find that the activity life cycle does not appear, only one way is


Then in the process of switching what you want to deal with is your own things, the state without saving, are still original, if you want to do some switching, you can get to the current screen status by the following code, and then operate on their own.

if (newconfig.orientation = = Configuration.orientation_landscape) {//do something} else if (newconfig.orientation = = Con Figuration. orientation_portrait) {//Do something}

If you just add the following, you will find that the activity will still go through the full life cycle, and the Onconfigurationchanged method will not perform at all.

android:configchanges= "Orientation"

This is because from the beginning of Android 3.2 (API 13), the screen switch is not only orientation will change, screensize will also change, so change to the following. Android:configchanges= "Orientation|screensize" tells the activity, whether it is orientation change or screensize change, do not reinitialize, All through onconfigurationchanged let me do it myself.
The above is to not let the activity reinitialization, but instead of self-adjustment to complete the screen switch adaptive operation.



Programme III :

The simplest and most brutal, prohibit the vertical and horizontal screen conversion, directly fixed to a flat screen or portrait screen.

The following configuration can be performed where the activity is configured

or inside the oncreate.
Setrequestedorientation (Activityinfo.screen_orientation_landscape); Orsetrequestedorientation ( activityinfo.screen_orientation_portrait);

This will ensure that the vertical screen is always vertical screen, or horizontal screen is always horizontal screen.

Above, does not involve the state preservation and the layout adapts, regardless of horizontal screen vertical screen everything as ever.


Reference:

Http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1106/516.html

http://blog.csdn.net/aliang775/article/details/8561075



jason0539

Weibo: http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)


The life cycle of the activity of Android development and the resource adaptation scheme when the screen is switched

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.