Android Test pits--screen flipping

Source: Internet
Author: User

Background

Recently looking at Android development, found that there are some pits that have not been understood by Android development is not discoverable, simply record it.

Scene

Using Android to develop an app to answer questions, there are about 5 questions, switch to the next question after the answer, or manually skip to the next question. The whole app is simple, but there's still a lot of pits.

The problem is reset to the first question after flipping the screen

This question is a very interesting question, in the vertical screen when looking at the fourth, but after the horizontal screen, the problem is reset to the first question.

To understand this problem, you must understand the activity's declaration cycle in Android. The activity in Android has the following life cycle: OnCreate, OnStart, Onresume, OnPause, OnStop, and OnDestroy.

    • Understand the changes in Android life cycle

When an activity is started, OnCreate, OnStart, and Onresume are activated. The state that the end user sees is Onresume, which gets the user's focus and allows the user to interact with the system. The log is typed as follows:

 A- -  -: the:22.714 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:oncreate (Bundle) called A- -  -: the:22.754 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:onstart () called A- -  -: the:22.754 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:onresume () called

This time, turn the screen upside down, log information as follows:

 A- -  -: -:37.354 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:ncreate (Bundle) called A- -  -: -:37.404 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:onstart () called A- -  -: -:37.404 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:onresume () called A- -  -: -:39.494 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:onpause () called A- -  -: -:39.494 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:onstop () called A- -  -: -:39.494 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:ondestroy () called A- -  -: -:39.514 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:ncreate (Bundle) called A- -  -: -:39.514 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:onstart () called A- -  -: -:38.514 22987-22987/com. Example. Svenweng. GeoquizD/quizactivity:onresume () called

From the log we can clearly see in the execution of the rollover, the system to the original activity was destroyed processing, that is, the execution of OnPause, OnStop and OnDestroy. After that, OnCreate, OnStart and Onresume were executed. Re-created an activity.

    • Analyze problems

From the code point of view, we control the problem is through a problem number to deal with, that is, in the OnCreate, the initialization of the number of the variable is 0, so in the vertical screen when the change is normal to see the topic, and once the screen is flipped, the OnCreate method will be re-executed, That is, the number of the variable is initialized to 0, so in the horizontal screen, we will find that the topic has become the first question.

    • Fix the problem

This problem can be solved by storing the variable of the question number when the screen changes.

    • Derivative expansion

When the activity involves horizontal screen vertical screen changes, if there are intermediate variables to record some data, you need to do storage operations, in the process of testing this piece of content, you need to specifically design for this issue.

The layout is not displayed correctly after flipping the screen

The interface that we see normally is written in the XML layout file with the code. In general, this layout is for the vertical screen state. If the page flips to a horizontal screen, the Android system automatically makes the horizontal screen fit according to the layout file of the vertical screen, and this adaptation sometimes appears inconsistent with the expected control distribution.

    • Analyze problems

The automatic adaptation of the Android system is beyond our control, and the problem involves the bottom of the Android system.

We can do the analysis according to the page, can be developed specifically for the characteristics of the horizontal screen to write a horizontal screen layout file, so that the layout of the screen after the horizontal screen layout file to control, in order to achieve the effect we want

Another way can be determined according to the requirements, whether users are prohibited from horizontal screen operation.

    • Derivative expansion

Android has an ever-changing resolution, running apps on a tablet may also cause the layout to look uncomfortable, or you can evaluate the need for specialized layout processing for these

Summarize

In the process of Android screen flipping is more prone to strange problems, and the process of flipping is prone to some minor problems, and these problems are often easily overlooked.

During the testing process, it is best to analyze each page, whether it is allowed to flip, whether there are intermediate variables in the page, and whether there are any saved during the rollover process.

Android Test pits--screen flipping

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.