Files
mkdocs/docs/android面试/开源框架/Jetpack组件.md
2026-01-15 11:53:37 +08:00

255 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Jetpack组件
## 目录
- [Lifecycle](#lifecycle)
- [ViewModel](#viewmodel)
- [LiveData](#livedata)
- [Room](#room)
- [Navigation](#navigation)
- [WorkManager](#workmanager)
- [Paging](#paging)
- [DataBinding](#databinding)
- [Jetpack最佳实践](#jetpack最佳实践)
- [面试常见问题](#面试常见问题)
---
## Lifecycle
### Lifecycle 简介
```java
// Lifecycle生命周期感知组件
// 自动管理生命周期相关的操作
```
### 使用方式
```java
// 实现 LifecycleObserver
public class MyObserver implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
// Activity onResume 时调用
}
}
// 注册观察者
lifecycle.addObserver(new MyObserver());
```
---
## ViewModel
### ViewModel 简介
```java
// ViewModel保存 UI 相关数据
// 在配置变更时保持数据
```
### 使用方式
```java
public class MyViewModel extends ViewModel {
private MutableLiveData<String> data = new MutableLiveData<>();
public LiveData<String> getData() {
return data;
}
public void setData(String value) {
data.setValue(value);
}
}
// Activity 中使用
MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
viewModel.getData().observe(this, data -> {
// 更新 UI
});
```
---
## LiveData
### LiveData 简介
```java
// LiveData可观察的数据持有者
// 自动更新 UI
// 生命周期感知
```
### 使用方式
```java
MutableLiveData<String> liveData = new MutableLiveData<>();
liveData.observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
// 数据变化时自动更新
}
});
liveData.setValue("New Value");
```
---
## Room
### Room 简介
```java
// RoomSQLite 数据库抽象层
// 编译时检查
// 支持 LiveData、RxJava
```
### 使用方式
```java
@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract UserDao userDao();
}
@Dao
public interface UserDao {
@Query("SELECT * FROM user")
LiveData<List<User>> getAllUsers();
}
```
---
## Navigation
### Navigation 简介
```java
// Navigation导航组件
// 简化 Fragment 导航
```
### 使用方式
```java
// 导航到目标
Navigation.findNavController(view)
.navigate(R.id.action_fragmentA_to_fragmentB);
```
---
## WorkManager
### WorkManager 简介
```java
// WorkManager后台任务管理
// 保证任务执行
// 支持约束条件
```
### 使用方式
```java
WorkRequest workRequest = new OneTimeWorkRequest.Builder(MyWorker.class)
.setConstraints(new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build())
.build();
WorkManager.getInstance(context).enqueue(workRequest);
```
---
## Paging
### Paging 简介
```java
// Paging分页加载数据
// 自动处理分页逻辑
```
---
## DataBinding
### DataBinding 简介
```java
// DataBinding数据绑定
// 减少 findViewById
// 自动更新 UI
```
### 使用方式
```xml
<layout>
<data>
<variable name="user" type="User" />
</data>
<TextView
android:text="@{user.name}" />
</layout>
```
---
## Jetpack最佳实践
### 1. 使用 ViewModel 保存数据
```java
// 配置变更时保持数据
```
### 2. 使用 LiveData 更新 UI
```java
// 自动更新,生命周期感知
```
### 3. 使用 Room 管理数据
```java
// 类型安全,编译时检查
```
---
## 面试常见问题
### Q1: ViewModel 的作用?
**答案:**
- 保存 UI 相关数据
- 在配置变更时保持数据
- 与 Activity/Fragment 生命周期分离
### Q2: LiveData 的特点?
**答案:**
- 可观察
- 生命周期感知
- 自动更新 UI
### Q3: Room 的优势?
**答案:**
- 编译时检查
- 类型安全
- 支持 LiveData、RxJava
---
*最后更新2024年*