Android interview questions - kotlin related interview questions

The main knowledge points of an interview

  1. kotlin coroutine
  2. Coroutine exception handling
  3. Higher order functions in kotlin
  4. with, run, apply, let functions in kotlin
  5. The size of the scanning area in Android (resolution-custom)
  6. Multilingual adaptation (multilingual placeholder)
  7. Static Proxy and Dynamic Proxy
  8. Android network access framework (Okhttp+retrofit)
  9. Introduction to Jetpack Compose Components
  10. constraint layout
  11. thread interrupt
  12. VMC/MVP/MVVM
  13. Project highlights and difficulties
  14. Communication between flutter and Android

Answers to the second interview questions

2.1 kotlin coroutine

1- Coroutines are our recommended solution for asynchronous programming on Android. Its features include:

  • Lightweight
  • fewer memory leaks
  • built-in cancellation support
  • jetpack integration

2- Create a coroutine

// 方法一,使用 runBlocking 顶层函数
runBlocking {
    get(url)
}

// 方法二,自行通过 CoroutineContext 创建一个 CoroutineScope 对象,通过launch开启协程
val coroutineScope = CoroutineScope(context)
coroutineScope.launch {
    get(url)
}

// 方法三,使用 GlobalScope 单例对象,GlobalScope 实际是CoroutineScope的子类,本质是CoroutineScope
GlobalScope.launch {
    get(url)
}

//方法四,使用async开启协程
GlobalScope.async {
      get(url)
}

3- Coroutine use

The most commonly used function of coroutines is concurrency, and the typical scenario of concurrency is multithreading.

You can use Dispatchers.IOthe parameter to switch the task to the IO thread for execution

coroutineScope.launch(Dispatchers.IO) {
    ...
}

Dispatchers.MainSwitch to main thread with

coroutineScope.launch(Dispatchers.Main) {
    ...
}

4- Coroutine suspension (suspend)

2.2 Coroutine exception handling

  • The exception of the coroutine is generally handled by using try/catchor built-in functionrunCatching
  • The second way for coroutines to handle exceptions is to useCoroutineExceptionHandler

2.3 Higher-order functions in kotlin

1-Definition

Higher-order function definition: a function whose parameters have a function type or whose return value is a function type.

2- Higher order function example

(String, Int) -> Unit

3- There are three usages of higher order functions

  • double colon::method
  • anonymous function
  • Lambda expressions

4- Higher-order functions commonly used in the system

  • map
  • flatMap
  • reduce
  • fold
  • joinToString
  • filter

2.4 with, run, apply, let functions in kotlin

1- Basic introduction:

  • with: It is not an extension function of T. Objects need to be passed in. It cannot be judged as empty. The last line is the return value.
  • run: It is an extension function of T, which uses this internally, and the last line is the return value.
  • apply: It is an extension function of T, which uses this internally, and the last line returns itself.
  • let: is an extension function of T, it is used internally, of course, the name can be customized (by modifying the parameters of the lambda expression), and the last line is the return value.
  • also: It is an extension function of T, which uses it internally like let, and the last line returns itself.

2- Usage scenarios

  • Used to initialize objects or change object properties, you can use apply
  • If the object is validated before assigning data to the receiving object's properties, use also
  • If the object is null-checked and its properties are accessed or modified, use let
  • If you want to calculate a value, or limit the scope of multiple local variables, use run

3- Difference

function Is it an extension function function parameters (this, it) return value (the call itself, the last line)
with no this the last line
T.run yes this the last line
T.let yes it the last line
T.also yes it call itself
T.apply yes this call itself

2.5 The size of the scanning area in Android (resolution-custom)

There is a class CameraManager in the Zxing package, which is used to set the size of the scanning frame

Scan frame initialization value

 private static  int MIN_FRAME_WIDTH = 240;

  private static  int MIN_FRAME_HEIGHT = 240;

  private static  int MAX_FRAME_WIDTH = 480;

  private static  int MAX_FRAME_HEIGHT = 360;

There is a getFramingRect method in this class to set the size of the scanned frame. If you want to modify the size of the scanned frame, you can modify it in this method

2.6 Multilingual adaptation (multilingual placeholder)

%s: string type

%d: integer type

%f: Floating point type

2.7 Static proxy and dynamic proxy

  • Static proxy: The source code is automatically generated by a program or a specific tool. Before the program runs, the .class file of the proxy class already exists
  • Dynamic proxy: when the program is running, it is dynamically created by using the reflection mechanism, no need to manually write code

2.8 Android network access framework (Okhttp+retrofit)

OkHttp interceptor chain

  • RetryAndFollowUpInterceptor: retry and failure orientation interceptor, which will create a StreamAllocation object to be passed to the subsequent interceptor
  • BridgeInterceptor: Bridge and Adaptation Interceptor
  • CacheInterceptor: cache interceptor
  • ConnectInterceptor: connection interceptor, establishes available connections, is the basis of the following interceptors
  • CallServerInterceptor: Responsible for writing our request into the network stream, don't read the data returned by the server to our client from the IO stream

2.9 Jetpack Compose component introduction

Jetpack is a rich component library, and its component library is divided into 4 categories by category

1- Architecture (Architecture)

  • Data Binding
  • Lifecycles
  • LiveData
  • Navigation
  • Paging
  • Room
  • ViewModel
  • WorkManager

2- Interface (UI)

  • Animation & Transitions
  • Auto,Tv&Wear
  • Emoji
  • Fragment
  • Layout
  • Palette

3- Behavior

  • Download Manager
  • Media&Playback
  • Permissions
  • Notifications
  • Sharing
  • Slices

4- Foundation

  • AppCompat
  • KTX Android
  • Multidex
  • Test

2.10 ConstraintLayout (ConstraintLayout)

ConstraintLayout is a ViewGroup, which appeared to solve the problem of excessive layout nesting (layouts within layouts) in complex layouts (nesting layouts will increase the time required to draw the interface). It can define the position for each view according to the constraints of sibling views and parent layout, similar to RelativeLayout, all views are laid out according to the relationship between sibling views and parent layout, but compared with RelativeLayout, it is more flexible , which is easier to use

  • Common Properties of Constraint Layout
  • Reference Line (GuideLine)
  • Chains
  • About view gone

2.11 Thread interruption

  • stop
  • interrupt
  • flag bit

2.12 VMC/MVP/MVVM

  • MVC(Model-View-Controller)
  • MVP(Model-View-Presenter)
  • MVVM(Model-View-ViewModel)

2.13 Project Highlights and Difficulties

Combined with item description

2.14 communication between flutter and Android

  • MethodChannel
  • BasicMessageChannel
  • EventChannel

Guess you like

Origin blog.csdn.net/Calvin_zhou/article/details/127525933