Android app development zero-based introductory training! Tencent Android development interview record, detailed technical introduction

beginning

It’s said that programmers are eating youth meals. This is indeed a bit right. I didn’t think so before, but as I grow older, the facts tell me that it is true. After 30, I will find that my body is different. In terms of indicators decline, physical and mental strength can’t keep up. This age is often embarrassing. Compared with young programmers, the output is no one’s high, but the company still pays you a high salary. Why can the company pay high wages if there is no better performance? Therefore, this group of people has entered the so-called mid-life crisis. In order to help this group of friends successfully tide over the mid-life crisis, I probably sorted out the following ideas.

The interviewer asked a question. Let’s take a look at the performance of the three students A, B and C.

A

Interviewer: Talk about the role of Application.

A : Application is a system component that will be created after the application process is created, so it can be used to do some initialization operations; Application life cycle is as long as the application process, so it can be used to provide Context to the class library; because all Context can be obtained Application can be used to save and transfer global variables.

Interviewer : Do you usually put global variables in Application? What if the application is recycled in the background and the value is lost when it is reopened?

A : Yes, it's very convenient, just do a fault-tolerant judgment

Interviewer : Okay, go back and wait for the notice


B

Interviewer : talk about the understanding of Application

B : Role: do initialization and provide context. In addition, Application is a Context, which directly inherits ContextWrapper; the member variable mBase of this ContextWrapper can be used to store the ContextImpl implemented by the system, so that when we call the Context method of Application, we will finally call the method of ContextImpl through a static proxy. . We can get an instance of ContextImpl by calling the getBaseContext method of ContextWrapper

Interviewer : Do you usually put global variables in Application? What if the application is recycled in the background and the value is lost when it is reopened?

B : No, save global variables with static variables, or singletons can gather them in a more suitable location.
To avoid data loss when the application is recycled, when you can pass parameters on the page, pass the parameters through the Intent, so that it is worthwhile to open and retrieve the parameters from the Intent after being recycled. If the amount of data is large, you can also consider data persistence; another method is to save the corresponding data when it is recycled and restore the data when it is reopened through onSaveInstanceState and onRestoreInstanceState.

Interviewer : Tell me about the life cycle of Application

B : Compared to Activity, the life cycle of Application is not too simple. The constructor is called when it is first created, and then the system is ready to inject the ContextImpl into the Application through the attachBaseContext( Context) method, and then call the onCreate method we are most familiar with. There is also an onTerminate method in the API that will call back when the process is killed, but it only takes effect in the simulator, so you don't need to pay attention.

Interviewer : Can you continue to talk about the initialization process of Application?

B : Basically those mentioned above, I didn’t understand it in detail.

Interviewer : Okay, go back and wait for the notice


C

Interviewer : Talk about the initialization process of Application

C : Application is initialized after the application process is created:

ActivityThread calls the attachApplication method of the Binder object (IActivityManager) of
AMS. After receiving the request, AMS calls the bindApplication method of
ActivityThread. ActivityThread This side receives the request and then assembles an AppBindData object, encapsulates all the parameters, and sends it to the main thread for execution through the handler

The main thread loops to this message and calls handleBindApplication to actually process the initialization Application

When handleBindApplication and we talked about "Context", the initialization of Activity is similar. Recap:

ClassLoader loads the Application class, instantiates
the ContextImpl used to initialize the Applicaction
through the Application.attach( Context) method, calls attachBaseContext( Context) to inject the ContextImpl into the Application and
finally calls Application.OnCreate()

In this way, Application is initialized

Interviewer : Why didn’t the process creation be directly transferred to handleBindApplication to create the Application, and then went to AMS for a circle

C : The attachApplication of AMS is called not only to create the Application, but also the four major components of the application may be called before the process is created but cannot be started; now that the process is created, the application has to be created to deal with these components to be started. Therefore, it needs to be uniformly scheduled through AMS. If the application creation and onCreate callback takes time, it will also affect the startup time of these components to be started

Interviewer : Yes, let's talk about other things.

At last

If you see this and think the article is well written, give it a thumbs up? If you think there is something worth improving, please leave me a message. Will surely inquire carefully and correct deficiencies. Thank you.

Finally put a benefit at the end of the article: GitHub address

PS: I have a lot of high-level Android learning video materials and interview materials in GitHub~

BF%E9%AB%98%E8%96%AA%EF%BC%81.md)**

PS: I have a lot of high-level Android learning video materials and interview materials in GitHub~

Welcome everyone to communicate and discuss together~

Guess you like

Origin blog.csdn.net/Sunbuyi/article/details/114138813