First, Android basics

Android basic knowledge is more, look at the picture.

Recommended reading:

Exploring the Art of Android Development

1. Activity

What are the four startup modes of Activity and the application scenarios?

There are four launch modes for an Activity:

  • standard: standard mode, each time a new one is generated in the active stackActivityInstance. Usually the activities we use are standard patterns.
  • singleTop: top of stack reuse, ifActivityInstances are already at the top of the stack, so no new instances are created in the active stack. A common scenario is for notifications to jumpActivityBecause you certainly don’t want the front deskActivityIs theActivityClick on the notification and create the same one for you againActivity.
  • singleTask: stack reuse, ifActivityIf the instance already exists in the current stack, the current will be changedActivityInstance above the othersActivityInstances remove stacks. Common for jumping to the home screen.
  • singleInstanceIn single-instance mode, a new task stack is created, and the activity instance sits alone in the activity stack.

What is the difference between onStart and onResume in # Activity? The difference between onPause and onStop?

First, there are three types of activities:

  • The front deskActivityActive:ActivityIs interacting with the userActivity.
  • Visible but not front deskActivity: Common at the top of the stackActivityThe background is transparent, underneath itActivityVisible but not interacting with the user.
  • The backgroundActivity: Has been suspendedActivityFor example, it has been executedonStopMethods.

So, onStart and onStop generally refer to whether the current activity is in the foreground, while onResume and onPause refer to whether the activity is visible.

2. Screen adaptation

How do you usually use screen adaptation? How does that work?

The usual screen adaptation of the general use of the headline screen adaptation scheme. Simply put, use one side of the screen as an adaptor, usually wide.

Principle: The relationship between device pixel PX and device independent pixel DP is

px = dp * density
Copy the code

Assuming that the screen width of the design drawing given by UI is based on 360DP, then the pixels of the device width are known, i.e. Px, dp is also known, 360DP, so density = px/dp. Then modify the knowledge points related to density in the system according to this.

3. Android messaging mechanism

Introduction to Android messaging?

Four concepts in Android messaging:

  • ThreadLocal: Data stored by the current thread can only be retrieved from the current thread.
  • MessageQueue: message queue with time priority.
  • Looper: polls the message queue to see if new messages arrive.
  • Handler: where logic is handled concretely.

Process:

  1. Preparation: CreateHandlerIf it is created in a child thread, it needs to be calledLooper#prepare()In theHandlerWill be bound toLooperandMessageQueue.
  2. Send a message: Creates a message, usingHandlerTo send.
  3. Enter theMessageQueueBecause:HandlerIs bound to message queues, soMessageIt is naturally placed in the message queue.
  4. LooperPolling message queues:LooperIt’s an endless loop, always watching for new messages to arrive, and then fromMessageUnboundHandler, and finally callHandlerIn the processing logic, this all happens inLooperLoop thread, this is alsoHandlerThe reason for being able to process tasks in the specified thread.

# Looper: why doesn’t the interface freeze due to an endless loop in the main thread?

  1. What causes it to get stuck is a time-consuming operation in the Ui thread that causes the interface to drop frames, or evenANR.Looper.loop()The operation itself will not cause this.
  2. Some people might say, if I put an infinite loop in the click event, it will cause the interface to freeze. It’s the same infinite loop, isn’t it? Looper blocks the current thread when there is no message, frees CPU resources, and wakes up the main thread when a message arrives.
  3. You need an infinite loop in your App, and if the loop ends, your App ends.

# IdleHandler?

IdleHandler is a mechanism for handling idle tasks when Hanlder is idle.

Execution scenario:

  • MessageQueueWhen there is no message and the queue is empty.
  • MessageQueueIs a delayed message, when no message is currently being executed.

Does an infinite loop occur: The answer is no, MessageQueue uses counting methods to ensure that a call to MessageQueue#next will only use the collection of IdleHandlers once.

4. View event distribution mechanism and View drawing principle

Gangul’s “Exploring the Art of Android Development” is already very comprehensive, recommended reading.

5. Bitmap

How to calculate Bitmap memory?

Given the length and width of the image in pixels, factors that affect memory size include resource file location and pixel size.

Pixel size: Common pixels are:

  • ARGB_8888: four bytes
  • ARGB_4444, ARGB_565:2 bytes

Location of resource files: Folders corresponding to different DPi files

For example, an image with a pixel size of 180*180px and a DPI (device-independent pixel density) of 320 is stored only in drawable- hdPI:

Horizontal pixels = 180 * 320/240 + 0.5F = 240 px Vertical pixels = 180 * 320/240 + 0.5f = 240 pxCopy the code

If it is stored only in drawable-xxhdpi, then:

Horizontal pixels = 180 * 320/480 + 0.5f = 120 px Vertical pixels = 180 * 320/480 + 0.5f = 120 pxCopy the code

So, for a 180*180px image, the device DPI is 320, the resource image only has drawable-hdpi, the pixel size is ARGB_4444, and the resulting file memory size is:

Horizontal pixels = 180 x 320/240 + 0.5F = 240 px Vertical pixels = 180 x 320/240 + 0.5F = 240 px Memory size = 240 x 240 x 2 = 115200 bytes 112.5 KBCopy the code

Efficient loading of bitmaps?

The efficient loading of Bitmap is also used in Glide.

  1. Get the required length and width, generally get the length and width of the control.
  2. Set up theBitmapFactory.OptionsIn theinJustDecodeBoundsTrue, which helps us get it without loading it into memoryBitmapThe length and width of.
  3. Compare the desired length and width with the length and width of the Bitmap to get the compression ratioBitmapFactory.OptionsIn theinSampleSizeProperties.
  4. Set up theBitmapFactory.OptionsIn theinJustDecodeBoundsFalse loads the image into memory and sets it into the control.

Second, Android advanced

Android advanced focuses on the Android Framework, performance optimization, and third-party frameworks.

1. Binder

Introduction to Binder? Advantages and disadvantages of other IPC methods?

Binder is an IPC method that is unique to Android, to quote from The Art of Android Development Explorer (with some modifications) :

Binder is a cross-process communication method in Android from an IPC perspective. A Binder is also a virtual physical device whose device driver is /dev/binder. In terms of the Android Framework, Binder is the bridge between Service Manager and ManagerService. In both the object-oriented and CS models, clients communicate with remote servers through binders.

With Binder, Android also implements other IPC methods, such as AIDL, Messenger, and ContentProvider.

Comparison with other IPC:

  • High efficiency: With the exception of memory sharing, IPC requires two copies of data, but Binder uses memory mapping to make only one copy.
  • Good security: The receiver can obtain the process Id and user Id from the packet to facilitate the verification of the identity of the sender. Other IPC can only take the initiative to save the data, but this may be modified in the process of sending.

The last

I have seen many technical leaders meet older programmers who are in a confused period, older than the interviewers. These people have a few things in common: they’ve been working for seven or eight years, they’re still writing code for the business department every day, they’re doing a lot of repetitive work, they’re not doing a lot of technical work. Ask these people about their career plans, and they don’t have many ideas.

In fact, the age of 30 to 40 is the golden stage of one’s career development. It is necessary to have a plan for the expansion of business scope, the breadth of technology and the depth of improvement, so as to have a sustainable development path in career development and not to be stagnant.

Keep running and you’ll know what learning is all about!

The above advanced BATJ factory learning materials can be shared with you free of charge, need a complete version of friends,You can see it all here】.