Fragment interface overlap problem in android horizontal screen

Source: Internet
Author: User

Objective:

Project is based on the development of the tablet, the design of the interface is to require a horizontal screen display interface. So I've forced all the activity to be set to horizontal screen

android:screenorientation="landscape"

-----------------------------------------------------------------------------------------------------

Problem:

The main interface, the most common activity+n a fragment

I use the hide, show fragment way to switch fragment, when the phone is turned off, the tablet screen and then open, you will find fragment interface overlap

Android Development _ MU Class network _fragment implementation tab (app main interface)

-----------------------------------------------------------------------------------------------------

1, first through the interface to understand the specific situation

The left side of the interface is 4 tab buttons, corresponding to four fragment interface, such as the following two graphs, is the normal interface display effect, here I have to force this activity for horizontal screen display

2, under normal circumstances, I closed the screen at this time, and then open the screen, you can see this time will show the previous open fragment interface, resulting in interface overlap

At this point, we test the activity of horizontal screen display removed, let him vertical display, and then on the screen, open screen operation, you will find that in the case of vertical screen, there is no interface overlap problem.

Then we can guess what the problem is: life cycle

3. Then we print log to the activity's OnCreate () Onresume () method fragment Oncreateview () Onresume () method

(1) Look at the life method we performed when we turned off the screen.

As we can see here, when we turn off the screen, it's clear that the activity has performed the life cycle again.

(2) The life method that we perform when we turn on the screen

  

As you can see, the activity executes another oncreate () method, and fragment executes multiple oncreateview ()

Summary: After the lock screen off the screen, activity will be changed from horizontal screen to vertical screen, this time the OnCreate () method is executed, but the original activity corresponding to the fragment is not destroyed, (resulting in the first life cycle diagram Today task fragment executed two times Onresume, because a new one and the original was not destroyed, there are two today's task fragment), after the screen opened, the activity has vertical screen switch to horizontal screen (we force the setting of the horizontal screen display), The OnCreate () method was executed, causing another fragment to be created, but the original fragment was not destroyed (so in the second chapter life cycle diagram, we can see that there are 3 Onresume () Methods for today's task Fragement). The result is that all fragment that are not destroyed before the screen is turned on are displayed.

Workaround:

We don't allow the activity to go through the life cycle when switching screens, so set the following two properties in the manifest file for the activity

android:screenorientation="landscape"  android:configchanges="  Orientation|keyboardhidden|screensize"

  Note:more than 4.0 of configchanges need to add the screensize attribute , otherwise the toggle screen will still perform the life cycle

Fragment interface overlap problem in android horizontal screen

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.