Android implementation of horizontal and vertical screen switching example code

In recent years, I have been making apps for mobile phones and TV boxes, and I have hardly considered the problem of horizontal and vertical screen switching. It's easy to say that the horizontal screen remains unchanged. If you design a vertical screen for it, people won't have a chance to make it; It's hard to say about applications on mobile phones. Some interfaces are designed with horizontal and vertical screen compatibility, which may be to show that your functions are powerful. But according to the Convention, maybe it's also a designer's plan to save trouble. We just make a plan. Just like the current mainstream apps only have a vertical screen mode, such as wechat, JD and China Merchants Bank. I cut some pictures to show it.

But for applications like maps, maybe the horizontal screen will be more friendly. Please see the design of Tencent map as follows:

If you are careful, you will find that the style of the horizontal and vertical screens of the map is almost the same layout, and it is relatively easy to adjust. I always think so. Switching between horizontal and vertical screens is a very easy thing, but now I don't think so.

Because I'm thinking about interface design on Android Tablet. The screen is large, and the horizontal and vertical screens can provide different forms of expression. This is the difference between tablet and mobile phone. But in this way, things will not be so simple. Especially when there are threads working in the background on the interface for inputting a large amount of information, it is very difficult to do. Here are some of my explorations. I hope you can give me more comments.

Life cycle story

The process of horizontal and vertical screen switching will go through ondestroy and oncreate. Generally speaking, this activity is closed and there is a new activity. Then, the data of the previous activity certainly does not exist. Although the contents entered in your EditText are still in this switching, you must remember that the member variables are empty. Data preservation has become a problem.

Save the scheme of the entire activity

Add this configuration to the activity in the manifest to avoid re destruction and creation, as follows:

Don't be happy too early. Unless you don't need to change the layout of the horizontal and vertical screens, this scheme still has a lot of work to do.

You'd better save the data

Onretainnonconfigurationinstance() is deprecated. Let's use onsaveinstancestate instead. For different horizontal and vertical screen layouts, we just need to create a layout land folder with a layout file of the same name. When oncreate, the corresponding horizontal and vertical screen layout will be loaded automatically. In the process of horizontal and vertical screen switching, you will experience onsaveinstancestate, giving you a chance to save data:

Fetch data from oncreate:

A progress bar example on GitHub

Please take a look at this example. The horizontal and vertical screen switching can have its own layout. The work of the progress bar will not be interrupted during the switching process. Fragment is used. See here for details: https://github.com/alexjlockwood/worker-fragments

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>