Basic Questions for Android

Source: Internet
Author: User

Basic Questions for Android

1. lifecycle of the Activity during screen Conversion

1.1 create an Activity and print the lifecycle of the Activity.

1.2 run the Activity and obtain the following information:

 

OnCreate -->
OnStart -->
OnResume -->

1.3 when switching from crtl + f12 to landscape Screen

OnSaveInstanceState -->
OnPause -->
OnStop -->
OnDestroy -->
OnCreate -->
OnStart -->
OnRestoreInstanceState -->
OnResume -->

1.4 When you press crtl + f12 to switch to the portrait screen, the same log is printed twice.

OnSaveInstanceState -->
OnPause -->
OnStop -->
OnDestroy -->
OnCreate -->
OnStart -->
OnRestoreInstanceState -->
OnResume -->
OnSaveInstanceState -->
OnPause -->
OnStop -->
OnDestroy -->
OnCreate -->
OnStart -->
OnRestoreInstanceState -->
OnResume -->

1.5 modify AndroidManifest. xml, add android: configChanges = orientation to the Activity, and execute step 3.

OnSaveInstanceState -->
OnPause -->
OnStop -->
OnDestroy -->
OnCreate -->
OnStart -->
OnRestoreInstanceState -->
OnResume -->

1.6 execute Step 4 again and find that the same information will not be printed, but an onConfigChanged line will be printed.

OnSaveInstanceState -->
OnPause -->
OnStop -->
OnDestroy -->
OnCreate -->
OnStart -->
OnRestoreInstanceState -->
OnResume -->
OnConfigurationChanged -->

1.7 change android: configChanges = orientation in step 5 to android: configChanges = orientation | keyboardHidden. After step 3 is executed, only onConfigChanged is printed.

OnConfigurationChanged -->

1.8 Step 4

OnConfigurationChanged -->
OnConfigurationChanged -->

Summary:

① When the android: configChanges of the Activity is not set, the life cycle of the screen is re-called, the screen is executed once, and the screen is split twice.

② When setting the Activity's android: configChanges = orientation, the screen will be re-called for each lifecycle, and the screen will be executed only once during horizontal and vertical switching.

③ When setting the Activity's android: configChanges = orientation | keyboardHidden, the live cycle is not re-called and only the onConfigurationChanged method is executed.

2. How to refresh the View

3. What are the differences between class variables and function variables?

 

Class variables are also called global variables, and function variables are also local variables. They have different scopes. The global variable acts on the entire class, and the local variable acts on a method. After the method ends, the variable disappears.
4. Java gc garbage collection mechanism
Java GC (Garbage Collection, Garbage Collection, and Garbage Collection) mechanism is one of the main differences between Java and C ++/C. As a Java developer, generally, you do not need to write memory collection and garbage collection code to avoid Memory leakage and overflow issues, and do not need to fight like a C programmer. This is because there is an automatic memory management and garbage Cleaning Mechanism in the Java Virtual Machine. In summary, this mechanism marks the memory in the JVM (Java Virtual Machine) and determines which memory needs to be recycled. According to a certain collection policy, the memory is automatically recycled, never Stop (Nerver Stop) ensures the memory space in the JVM. Memory leakage and overflow may occur.
5. How can I solve the OOM problem in Andriod?

OOM may occur in the following situations:

① Large objects need to be loaded in the application, such as Bitmap

Solution: When we need to display large bitmap objects or a large number of bitmaps, We need to compress them to prevent OOM problems. We can set BitmapFactory. the inJustDecodeBounds attribute of Optiions is true, so that the image will not be loaded into the memory, but the width and height attributes of the image will be read. We can use this attribute to compress bitmap. Options. inSampleSize allows you to set the compression ratio.

② Useless objects cannot be gc, resulting in Memory Leak. That is, Memory leakage. There is no memory leakage at the initial stage, but it will eventually cause memory overflow.

6. How to Avoid ANR Problems

① The Activity main thread (event processing thread "/" UI thread) does not respond to the input event within 5 seconds

② BroadcastReceiver does not return results within 10 seconds

③ The Service is also running in the UI Thread, but it is larger than the threshold value of ANR reported by activity. If ANR is not returned within 15 seconds, an error occurs.

The solution is not to perform time-consuming operations in the UI thread. Generally, it is a promoter thread. to update the UI interface, you can use Handler and Message to complete the operation.

7. Static variables and static methods

The description of the static variable type is static.

Static variables are stored in static mode. The storage space is the static data area in the memory (the storage unit is allocated in the static storage area ), the data in this region keeps occupying the storage space during the entire program running (the storage space is not released during the entire program running), or the memory address remains unchanged, the entire program runs until the end (on the contrary, the auto automatic variable, that is, the dynamic local variable, belongs to the dynamic storage class, occupies the dynamic storage space, and is released after the function call ends ). Static variables always exist throughout the entire execution process of the program, but cannot be used outside the scope of the program.


Static variables do not mean that they cannot change the value, and the amount of values that cannot be changed is a constant. Its value is variable, and it will keep the latest value. It is static because it does not change with the function call and exit. That is, if we assign a value to a static variable during the last function call, the value remains unchanged during the next function call.

The static method uses the public memory space, that is, all objects can be directly referenced. You do not need to create an object before using this method.


 

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.