Android Basic question

Source: Internet
Author: User

1. The life cycle of the activity when the screen is turned

1.1 Create a new activity and print out the various life cycles

1.2 Run activity to get the following information

Oncreate-->
Onstart-->
Onresume-->

1.3 When switching Cheng by crtl+f12

Onsaveinstancestate-->
Onpause-->
Onstop-->
Ondestroy-->
Oncreate-->
Onstart-->
Onrestoreinstancestate-->
Onresume-->

1.4 When you press CRTL+F12 to switch to vertical screen, I find that the same log was printed two times.

Onsaveinstancestate-->
Onpause-->
Onstop-->
Ondestroy-->
Oncreate-->
Onstart-->
Onrestoreinstancestate-->
Onresume-->
Onsaveinstancestate-->
Onpause-->
Onstop-->
Ondestroy-->
Oncreate-->
Onstart-->
Onrestoreinstancestate-->
Onresume-->

1.5 Modify Androidmanifest.xml, add the activity android:configchanges= "orientation", perform step 3

Onsaveinstancestate-->
Onpause-->
Onstop-->
Ondestroy-->
Oncreate-->
Onstart-->
Onrestoreinstancestate-->
Onresume-->

1.6 Perform step 4 again to find that the same information is not printed, but one line is printed more onconfigchanged

Onsaveinstancestate-->
Onpause-->
Onstop-->
Ondestroy-->
Oncreate-->
Onstart-->
Onrestoreinstancestate-->
Onresume-->
Onconfigurationchanged-->

1.7 Change the android:configchanges= "orientation" of step 5 to Android:configchanges= "Orientation|keyboardhidden" and perform step 3, Just print onconfigchanged

Onconfigurationchanged-->

1.8 Perform step 4

Onconfigurationchanged-->
Onconfigurationchanged-->

Summarize:

① do not set the activity of android:configchanges, the screen will recall the various life cycle, cut across the screen will be executed once, cut the vertical screen will be executed twice

② the android:configchanges= "orientation" of the activity is set, the screen will recall each life cycle and will only be executed once when cutting horizontally and vertically.

③ the android:configchanges= "Orientation|keyboardhidden" of the activity is set, the screen does not recall the individual lifecycles, only the Onconfigurationchanged method is executed

2. View how to refresh

3. What is the difference between a class variable and a function variable?

A class variable is also called a global variable, and a function variable is also a local variable, with different scopes. Global Variables Act on the entire class, local variables act on a method, and when the method ends, the variables disappear. 
4. Java GC garbage collection mechanism
java GC (Garbage Collection, garbage collection, garbage collector) mechanism, is one of the main differences between Java and C++/C, as a Java developer, In general, there is no need to specifically write memory recycling and garbage cleanup code, memory leaks and overflow problems, and do not need to be as jittery as C programmers. This is because there is an automatic memory management and garbage cleanup mechanism in the Java Virtual machine. In a nutshell, this mechanism marks the memory in the JVM (Java Virtual machine) and determines which memory needs to be recycled, automatically reclaims memory according to a certain recycling strategy, and never Stops (Nerver Stop) to ensure that the memory space in the JVM There is a memory leak and overflow problem with the placement. 
5. Andriod Encounter Oom problem, how to solve?

There are several situations that result in Oom:

Large objects need to be loaded in ① applications, such as bitmap

Solution: When we need to display large bitmap objects or more bitmap, we need to compress them to prevent oom problems. We can set the Injustdecodebounds property of Bitmapfactory.optiions to True so that the image is not loaded into memory, but the width and height properties of the image are read out, and we can use this property to compress the bitmap. Options.insamplesize can set the compression ratio

② holds useless objects so that it cannot be GC, resulting in memory Leak. That's what we're talking about. There is nothing early in the memory leak, but eventually it can cause memory overflow.

6. How to avoid several cases of ANR

①activity main thread (Event processing Thread "/" UI thread) does not respond to input events within 5 seconds

②broadcastreceiver did not complete the return within 10 seconds

③sErvice is also running in the UI thread, only more than the activity reported ANR threshold, 15 seconds did not complete the return, the emergence of ANR.

The workaround is to not take the time-consuming action in the UI thread, which is typically to start the child thread, and if you want to update the UI interface, you can do so with handler and message.

7. Static variables and static methods

static variable type specifier is static.

Static variables are static storage, where the storage space is a static data area in memory (a storage unit is allocated in a static store), and the data in that region occupies the storage space throughout the program's run (not released during the entire run of the program), or it can be considered as its memory address To the end of the entire program run (instead, the auto automatic variable, dynamic local variable, belongs to the dynamic storage category, which accounts for dynamic storage space, which is released after the function call ends). A static variable is always present throughout the execution of a program, but is not available outside its scope.


A static variable does not mean that it cannot change the value, and the amount of the value cannot be changed is called a constant. The value it has is mutable, and it maintains the most recent value. It is static because it does not change as the function calls and exits. That is, when the function was last called, if we assign a value to a static variable, the value remains unchanged the next time the function is called.

A static method uses a common memory space, meaning that all objects can be referenced directly, and no object needs to be created to use the method.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basic question

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.