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 Perform activity to get information such as the following

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

1.3 When switching Cheng by crtl+f12

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

1.4 Press CRTL+F12 to switch to vertical screen, found that two times the same log was printed

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

1.5 Change Androidmanifest.xml. Add the activity to android:configchanges= "orientation" and run step 3

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

1.6 Run Step 4 again. Find no more printing of the same information, but more than one line printed 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". Run Step 3. Just print onconfigchanged

Onconfigurationchanged-->

1.8 Running Step 4

Onconfigurationchanged-->
Onconfigurationchanged-->

Summarize:

① does not set the activity's android:configchanges, the screen will call each life cycle again, and it will run once when the screen is cut horizontally. Runs two times when you cut a vertical screen

② you set the activity's android:configchanges= "orientation", the screen will call each life cycle again. Only run once when cutting horizontal and vertical screen

③ the android:configchanges= "Orientation|keyboardhidden" of the activity is set, the screen will not invoke the individual lifecycles again, only the Onconfigurationchanged method will be run

2. How the view is refreshed

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 Java developers, there is generally no need to write memory and garbage cleanup code, memory leaks and overflow problems, and do not need to be as timid as the C program ape. This is due to the presence of active memory management and garbage cleanup mechanisms in the Java Virtual machine. In a nutshell. This mechanism flags the memory in the JVM (Java Virtual machine) and determines which memory needs to be recycled, based on a certain recycling strategy. Self-actively reclaim memory. Never-ending (nerver Stop) guarantees memory space in the JVM, where memory leaks and overflow issues occur.
5. Andriod Encounter Oom problem, how to solve?

There are several situations that cause oom:

① applications need to load large objects, such as bitmap

Solution: When we need to display large bitmap objects or more bitmap. Compression is required to prevent oom problems.

We can set bitmapfactory.optiions by setting the Injustdecodebounds property to True. This does not load the image into memory, but it will read the width and height properties of the image, 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 it can eventually 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 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 return, appeared ANR.

The workaround is not to take time-consuming actions in the UI thread. is typically the start of a child thread. Suppose you want to update the UI interface, you can use handler and message to complete.

7. Static variables and static methods

The static variable type descriptor is static.

Static variables belong to static storage mode. Its 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 execution of the program (not released during the entire execution of the program). It is also possible to feel that its memory address remains the same until the entire program execution finishes (instead. Auto's own active variable, dynamic local variable, belongs to the dynamic storage category, which is the dynamic storage space, which is released after the function call ends. Static variables persist throughout the execution of a program, but cannot be used outside of its scope.


A static variable does not mean that it cannot change the value. The amount of value that cannot be changed is called a constant. Its owning value 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, assuming that we assign a value to a static variable, the value remains the same when the next function call.

Static methods use public memory space, meaning that all objects can be referenced directly. You do not need to create objects to use this method.

8. Android handler mechanism

opening a child thread directly in the UI thread to update the contents of the TextView display, executing the program we will find, for example, the following error: Android.view.viewroot$calledfromwrongthreadexception:only The original thread that created a view hierarchy can touch it views. The only thread that creates this control is the ability to update the contents of the control. all UI threads are responsible for creating the view and maintaining it, such as updating the display of a textview, which must be done in the main thread, and we cannot directly create a child thread in the UI thread to update the UI interface, using the message mechanism: Handler. For example, here is the simple schematic diagram of handler:
1) Looper: A thread can produce a Looper object. It is the MessageQueue (Message Queuing) that manages this line of thread.


2) Handler: You can construct Handler objects to communicate with Looper in order to push new messages into the MessageQueue, or to receive messages sent by Looper from the message queue.
3) Message queue (Message Queuing): Used to hold messages placed by the thread.


4) Thread: Uithread is usually the main thread, and Android initiates a program to create a MessageQueue for it.

9. Differences in processes and threads

Process, which is often defined as the running of a program. The ability to think of a process as a separate program with complete data space and code space in memory.

The data and variables owned by a process belong to himself alone.


A thread, a program that executes in a single process.

That is, a thread exists in a process, and a process is composed of one or more threads. Each thread shares the same code and global data, but each has its own stack. Because the stack is one per thread, local variables are private to each thread. Because all threads share the same code and global data, they are closer than the process and the interaction between threads is easier. Because they have some shared memory for communication: Global data for the process.

A process has a complete virtual address space that is independent of the thread and is a part of the process. Without its own address space, share all resources assigned to the process with other threads in the process

Relationship of processes and threads
(1) A thread can only be part of a process. A process can have more than one thread, but at least one thread.
(2) Resources are allocated to processes. All the threads of the same process share all the resources of the process.
(3) The processor is assigned to a thread, that is, a thread that is actually executing on the processing machine.


(4) The thread is in the process of running. Collaborative synchronization is required. Synchronization is achieved between threads of different processes using the means of message communication.

10. Bits, Bytes, Chinese characters occupy space size

Bit means "bit" or "bit", which is the basis of computer operation;
byte means "byte", which is the basic computing unit of computer file size;
1byte = 8bits. The two conversions are 1:8 of the relationship.
Two bytes of a Chinese character.


1BIT=1/16 a Word
So 16bit=1 a Chinese character

11.Intent filter mechanism

12. The mechanism of AM

basic operation of Vim

14.Serializable and Parcelable differences
The problem with the serialization of your own defined objects in Android there are two choices one is parcelable and the other is serializable (Java).
A serialization reason:
① permanently saves the object, saving the byte sequence of the object into a local file.
② objects are passed through the network through serialized objects.
③ passes an object between processes through serialization.


The second is to choose which of the following principles:
① parcelable analogy serializable high performance when using memory, it is recommended to use the Parcelable class.
②serializable generates a large number of transient variables at serialization time, causing frequent GC.
③ if you want to store the data on disk, using serializable, because of the change in the outside world, parcelable can not be very good to ensure the continuity of data.


Implementation method:
The implementation of ①serializable. Only need to inherit implements Serializable. This simply marks the object and the system will serialize it on its own initiative.
The implementation of ②parcelabel. You need to include a static member variable CREATOR in your class. This variable needs to inherit the Parcelable.creator interface.

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.