preface

I want to join a big Internet company. I worked so hard to get an interview and passed the written test, but I failed in the technical interview, which I am best at. It is a pity that it is also a little unworthy of their hard work. So today I will share 70 technical interview questions on Android to help you bypass those technical pits!

In order to be able to answer the interview questions gracefully and honorably, this article draws on the description of knowledge points from different platforms.

  • If there is infringement please contact me
  • Please correct the deficiencies and mistakes in the article. Don’t be stingy with good suggestions. I will adopt and correct them

[Technical Interview question]

1. Activity lifecycle?

onCreate() -> onStart() -> onResume() -> onPause() -> onStop() -> onDetroy()

2. Service life cycle?

There are two ways to start a service: startService() and bindService(). Different startup modes have different life cycles.

StartService () calls startService() –> onCreate()–> onStartConmon()–> onDestroy(). This way to start, need to pay attention to a few problems, first: When we call startService,onCreate() will only be called once, and onStartConmon() will be called multiple times. When we call stopService(), OnDestroy () is called to destroy the service. OnStartConmon () : if the intent is null, the intent is null. If the intent is null, the intent is null.

BindService () is used to bind a service. BindService –>onCreate()–>onBind()–>unBind()–>onDestroy() bingService For example, there are several methods to add to a service. If you want to call them in an activity, you need to get the ServiceConnection object in the activity, and use ServiceConnection to get the class object in the service. This class object is then used to call methods in the class, which of course inherits from the Binder object

3. The Activity startup process (don’t answer the lifecycle)

There are two ways to start an app. The first way is to click the corresponding application icon on the desktop launcher, and the second way is to start a new activity by calling startActivity in the activity.

When we create a new project, the default root activity is the MainActivity, and all activities are stored on the stack, so when we start a new activity, we put it on top of the previous activity, and when we click the application icon from the desktop, Since the launcher itself is an application, when you click on the icon, the system calls startActivitySately(). Typically, information about the activity you start is stored in the intent, such as action, category, and so on. When we install this application, the system will also start a management service of PackaManagerService. This management service will parse the androidmanifest. XML file to get relevant information in the application, such as service, activity, Broadcast, etc., and then get information about the relevant components. When we click on the application icon, the startActivitySately() method is called, Inside the method, startActivty() is called, and the startActivity() method eventually calls the startActivityForResult() method. The startActivityForResult() method. Since the startActivityForResult() method returns a result, the system simply gives a -1 to indicate that the result is not needed. The startActivityForResult() method actually starts the activity with the execStartActivity() method in the Instrumentation class, The Instrumentation class is mainly used to monitor the interaction between the program and the system. In the execStartActivity() method, you get a proxy object for ActivityManagerService from which to start the activity. The startup will call a checkStartActivityResult() method that will throw an exception if the component is not configured in the configuration manifest. Finally, of course, is to call Application. ScheduleLaunchActivity () to start the activity, and this method by getting a ActivityClientRecord object, The ActivityClientRecord uses a handler to send messages. Each activity component is described using an ActivityClientRecord object. The ActivityClientRecord object holds a LoaderApk object that starts the Activity component by calling the handleLaunchActivity, from which the page lifecycle methods are called.

4. Broadcast registration methods and differences

Extended here: When to use dynamic registration

Broadcast Broadcast. There are two registration modes.

The first is static registration, which can also be a resident broadcast, which needs to be registered in androidmanifest.xml, and the broadcast registered in this way is not affected by the page life cycle, even if you exit the page, you can receive the broadcast which is usually used when you want to boot up and so on, Since this registered broadcast is a resident broadcast, it consumes CPU resources.

The second one is dynamic registration, and dynamic registration, when you register in code, is also called non-resident broadcasting, because when you get a life cycle effect, when you exit the page, you don’t get a broadcast, and we usually use that for UI updates. This registration mode has a higher priority. Finally, you need to unbind it to avoid memory leakage

Broadcasting is divided into orderly broadcasting and disordered broadcasting.

HttpClient and HttpUrlConnection

Which request method is used in Volley (HttpClient before 2.3, HttpUrlConnection after 2.3)

HttpClient and HttpUrlConnection both support Https. They upload or download data in the form of streams, or transfer data in the form of streams. They also support ipv6 and connection pooling. HttpClient has so many apis that it is difficult to extend it without breaking its compatibility, which is why Google scrapped it for Android6.0.

HttpUrlConnection is relatively lightweight, with fewer apis, easy to expand, and can meet most of the Android data transfer. Volley, a classic framework, used HttpClient until version 2.3 and HttpUrlConnection since.

6. Explain the Context

Context is an abstract base class. In the context of translation, it can also be understood as the environment, which provides basic information about the running environment of some programs. Context has two subclasses, ContextWrapper, which encapsulates the Context functionality, and ContextImpl, which implements the Context functionality. ContextWrapper has three direct subclasses, ContextThemeWrapper, Service, and Application. ContextThemeWrapper is a wrapper class with a theme, and it has a direct subclass Activity. Therefore, the Context of an Activity is different from that of a Service or Application. Only an Activity needs a theme. A Service does not require a theme. Context is of three types: Application, Activity, and Service. These three classes have different functions, but they are all part of the Context, and their Context functions are implemented by the ContextImpl class. Therefore, in most scenarios, The three types of Context, Activity, Service, and Application, are all generic. However, there are a few special scenarios, such as starting an Activity and popping up a Dialog. For security reasons, Android does not allow an Activity or Dialog to appear out of thin air, and one Activity must be launched on top of another, creating a return stack. A Dialog must pop up on top of an Activity (unless it’s a System Alert Dialog), so in this scenario we can only use an Activity-type Context, otherwise we’ll get an error.

The getApplicationContext() and getApplication() methods return the same application object, but of different types.

Number of contexts = Number of Activities + Number of Services + 1 (1 is Application)

7. Understand the relationship between Activity, View and Window

That’s a really hard question to answer. So here’s a good analogy to describe their relationship. An Activity is like a craftsman (a control unit), a Window is like a Window (a carrying model), a View is like a Window splint (a display View), layoutInflaters are like scissors, and Xml configurations are like a Window splint.

1: Activity construction initializes a Window, specifically a PhoneWindow.

2: This PhoneWindow has a “ViewRoot”, which is a View or ViewGroup, which is the original root View.

3: “ViewRoot” through the addView method to add views one by one. TextView, Button, etc

WindowManagerService receives the message and calls back the Activity function. Such as onClickListener, onKeyDown, etc.

10. Four LaunchModes and their usage scenarios

Extended here: the difference between stack (First In Last Out) and queue (First In First Out)

8. Save the Activity state

OnSaveInstanceState (Bundle) is called before the activity goes into the background state, that is, before onStop() and after onPause.

9. Several animations in Android

Frame animation: by specifying the picture and playing time of each frame, the animation effect is formed by orderly playing, such as the rhythm bar you want to hear.

Tween animation: by specifying the initial state, change time and method of View, a series of algorithms are used to transform graphics to form animation effects, mainly including Alpha, Scale, Translate and Rotate. Note: This animation is only implemented in the View layer, without really changing View properties, such as sliding lists and changing the transparency of the title bar.

Property animation: only supported on Android3.0, you can animate the View by constantly changing its properties and constantly redrawing it. The properties of the View are really changed compared to the View animation. For example, view rotation, zoom in, zoom out.

10. Several ways to communicate across processes in Android

Android can communicate across processes like IntEnts, ContentProviders, broadcasts, and Services.

Intent: This cross-process approach is not in the form of accessing memory; it requires passing a URI, such as making a phone call.

ContentProvider: This form uses the form of data sharing to share data.

Service: remote service, aidl

Other Android interview questions

Activity

OnSaveInstanceState (),onRestoreInstanceState (), onSaveInstanceState(),onRestoreInstanceState 14. To switch between vertical and horizontal screens, press the Home button and the Back button to lock and unlock the screen, jump to the transparent Activity interface, and start a Theme Dialog Activity. The difference between onStart and onResume, and between onPause and onStop is that the Intent is not limited in size. 17, When does the onNewIntent() method of the Activity execute? 18, Display startup and implicit startup How to transfer data between Activty. 23. How to start an Activity across Apps 26, How to save the Activity data, and how to restore the saved data after the process is killed

Service

What is the life cycle of a service? What is the difference between a service and an Activity? Application scenarios And the difference between onStartCommand and Service 29. How many returns does the onStartCommand method of Service have? What does each stand for? What is the life cycle of bindService and startService and how to disable it

BroadcastReceiver

Classification and Application scenarios of broadcast 32. Differences between the two registration modes of broadcast 33. Principles of sending and receiving broadcast 34

ContentProvider

35, what is a ContentProvider and its use 36, ContentProvider, ContentResolver, 37, the relationship between the ContentObserver ContentProvider the principle 39. What is a Uri

Handler

Handler Handler Handler Handler Handler Handler Handler Handler Handler Handler Handler Handler A thread can have several handlers, loopers, and MessageQueue objects The difference between POST and sendMessage of Handler and the application scenario Suppose there was a postDelay of 10s, then a postDelay of 1s, SendMessageDelayed -sendMessageAtTime-sendMessage 48, What is the data structure of MessageQueue 49, how does Handler make a thread corresponding to a Looper, How to ensure that there is only one MessageQueue ThreadLocal in the Handler mechanism What is the synchronization barrier mechanism for child threads to update the UI 54, Why does Android not recommend child threads to access the UI 55, why does Android not have the main thread because of the dead loop in looper.loop ()? 56. How does a looper distinguish between multiple handlers when an Activity has multiple handlers? What is the difference between Looper. Quit /quitSafely handling a thread and a callback Which of the associated

The View map

MeasureSpec = MeasureSpec = MeasureSpec = MeasureSpec = MeasureSpec = MeasureSpec = MeasureSpec = MeasureSpec 65, why does onCreate not get the width and height of the View 66, the difference between View#post and Handler#post 67, Android drawing and screen refresh mechanism 69, Choreography principles 69, what is double bufferingchoreography 70. Why SurfaceView

Write in the last

Due to space limitations, This article can’t answer all the Android technology interview questions (View event distribution, RecycleView, ViewPage &Fragment, WebView, Animation, Bitmap, MVC&MVP & MVVM, Binder, Memory Leak & Memory Overflow, performance optimization) , Windows &WindowManager, AMS, System startup, App startup & packaging & installation, serialization, Art & Dalvik and its differences, modularization & componentization, hot fix & Plug-in, AOP, JectPack, open source framework, etc. Click on my Gitee to get a free PDF version of the complete document

If this article can help you along the way, please share it with your likes!