Android develops a method of using activity to nest multiple fragments to realize the horizontal and vertical screen switching function

This paper describes the method of using activity nested multiple fragments to realize the horizontal and vertical screen switching function in Android development. Share with you for your reference, as follows:

1、 Above

2、 Demand

Recently, the project encountered a complex problem of horizontal and vertical screen switching, which is recorded here.

1. Three fragments are nested in the vertical screen in activity, which is referred to as vertical screen FP1, FP2 and FP3 in this paper. 2. Vertical screen FP1 and FP2 can be switched to horizontal screen FL1 and FL2, that is, vertical screen FP1 can be switched to corresponding horizontal screen FL1, and vertical screen FP2 can be switched to horizontal screen FL2. 3. FP3 does not allow horizontal and vertical screen switching. 4. Vertical screen FP1 and FP3 use viewpager to realize left-right sliding switching. 5. The horizontal screen FL1 and FL2 can be switched left and right with the switching buttons in the layout, and sliding switching is not allowed.

I feel a little dizzy when I see this demand!!! ha-ha!!!

(1) First, let's talk about the detours I have taken, and switch the horizontal and vertical screens in an activity.

(1) What is the difficulty in realizing horizontal and vertical screen switching in an activity? It is mainly horizontal and vertical screen switching. Activity has its own life cycle, and fragment also has its life cycle, and the life cycle of activity controls the life cycle of fragment. The most complicated thing is that the ondestoryview() method of the activity will be executed during the first horizontal and vertical screen switching. Before this method is executed, the ondestoryview() method of the fragment will be executed first, and then the oncreateview() method will be executed. When switching to the horizontal screen for the first time, the oncreate() method of activity will execute, and then the oncreateview() method of horizontal screen FL1 will execute again. In this way, the layout of the fragment will be overwritten. (this is a problem I found when I was doing it. I don't know if others have encountered it). (2) . in an activity, the vertical and horizontal screens are switched. The vertical screen layout is different from the horizontal screen layout. In this example, the vertical screen is nested with 3 fragments in the activity and the horizontal screen is nested with 2 fragments. First, there will be many page states to be recorded. Second, the life cycle relationship between the activity and the fragment is really complex. Control the state in this, Only those who have done it know how hard it is.

(2) It is advisable to switch the horizontal and vertical screens in two activities

3、 Problem solving ideas

1. First of all, of course, to enable two activities to switch horizontally and vertically, you need to configure two activities in androidmanifest.xml to switch horizontally and vertically. The configuration is as follows:

You can click here to view a more detailed description of Android permission settings

2. When switching from vertical screen activity to horizontal screen activity, jump to horizontal screen activity through intent, and then give the current activity to finish(). The opposite is the same logic. So what's the problem now? Where is the jump written? It's obviously inappropriate to write it in the ondestory () method, because this method will always be executed when cutting horizontally and vertically. When you press the back key to return to the previous page, this method will also be executed. In this way, if you jump, it will loop and always open the page. At this time, I think of the onconfigurationchanged () method of activity. Google's official website said that horizontal and vertical screen switching does not want you to use this method to achieve horizontal and vertical screen switching, but encountered such strange needs and had to use it. Upper Code:

3. After the horizontal and vertical switching between the two activities, we can gain a benefit. The life cycle of fragment is very easy to control. The logic in it can be written as it wants. Moreover, after the switching, the layout will automatically load the horizontal screen layout. I'm sure you know what I mean.

4. Switching to the corresponding fragment is mainly to record the page state with the help of cache. See the source code for details.

5. Main code on:

(1) Main logic of vertical screen activity

(2) Main logic of horizontal screen activity

6. Click here to download the complete example code.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android view skills, summary of activity operation skills of Android programming, summary of Android file operation skills, summary of Android resource operation skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>