当前位置:网站首页>Usage of ViewModel and livedata in jetpack
Usage of ViewModel and livedata in jetpack
2022-06-25 00:21:00 【BY-91】
List of articles
ViewModel Introduce
ViewModel Separate the data needed by the page from the page , The page only needs to deal with user interaction and presentation data . Is between View(UI) and Model( data ) The bridge between , So that the view and data can be separated , And keep the communication going .
ViewModel Independent of configuration changes , for example activity As you rotate, the page reconstructs , The life cycle will end and start again , But it won't affect ViewModel Life cycle of , Or that one? ViewModel.
ViewModel The instantiation process of is through ViewModelProvider To complete ,ViewModelProvider Will judge ViewModel Whether there is , If it exists weakly, it returns directly , Otherwise, create a new ViewModel.
ViewModel Instantiation
val timerViewModel = ViewModelProvider(this).get(TimerViewModel::class.java)
ViewModelProvider Method requires a ViewModelStoreOwner Parameters ,
public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {
this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory
? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory()
: NewInstanceFactory.getInstance());
}
We use the context of the current page this As ViewModelProvider Parameters of , This is because in the androidx Take our activity In the end, he inherited it ComponentActivity, And this activity It has been implemented for us by default ViewModelStoreOwner Interface
ViewModelStoreOwner Interface , This interface defines getViewModelStore Method and return ViewModelStore object
@SuppressWarnings("WeakerAccess")
public interface ViewModelStoreOwner {
/**
* Returns owned {@link ViewModelStore}
*
* @return a {@code ViewModelStore}
*/
@NonNull
ViewModelStore getViewModelStore();
}
ViewModelStore Maintained in object ViewModel, It can be seen that ViewModel In fact, it is based on HashMap<String, ViewModel> In the form of , So it's independent of activity There is a life cycle of , So it's using viewmodel Don't pass in any type of Context Or with Context Object reference of , As a result, the page cannot be destroyed , This causes a memory leak (activity At the time of destruction , Its own reference has not been broken and cannot be destroyed, which will cause a memory leak - Applied for a piece of memory , But it wasn't released in time , And this memory will be occupied all the time and can't be allocated any more )
public class ViewModelStore {
private final HashMap<String, ViewModel> mMap = new HashMap<>();
final void put(String key, ViewModel viewModel) {
ViewModel oldViewModel = mMap.put(key, viewModel);
if (oldViewModel != null) {
oldViewModel.onCleared();
}
}
final ViewModel get(String key) {
return mMap.get(key);
}
Set<String> keys() {
return new HashSet<>(mMap.keySet());
}
/**
* Clears internal storage and notifies ViewModels that they are no longer used.
*/
public final void clear() {
for (ViewModel vm : mMap.values()) {
vm.clear();
}
mMap.clear();
}
}
If in ViewModel You have to use context, have access to AndroidViewModel class , It is ViewModel Subclasses of , receive Application As Context, It also means that its life cycle is the same as application Same .
ViewModel Source code creation
public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) {
ViewModel viewModel = mViewModelStore.get(key);
if (modelClass.isInstance(viewModel)) {
if (mFactory instanceof OnRequeryFactory) {
((OnRequeryFactory) mFactory).onRequery(viewModel);
}
return (T) viewModel;
} else {
//noinspection StatementWithEmptyBody
if (viewModel != null) {
// TODO: log a warning.
}
}
if (mFactory instanceof KeyedFactory) {
viewModel = ((KeyedFactory) mFactory).create(key, modelClass);
} else {
viewModel = mFactory.create(modelClass);
}
mViewModelStore.put(key, viewModel);
return (T) viewModel;
}
Use
rely on
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0'
viewmodel Is an abstract class , There's a... In it onCleared() Method , When viewmodel No longer needed , That is, the related activity Be destroyed when the , This method is called by the system . You can perform some resource release operations , But because of the rotation of the screen activity The reconstruction of does not trigger this method , Does not affect the viewmodel Life cycle of
Let's write a viewmodel Implementation class of TimerViewModel, Maintain a timer inside , And then in activity Instantiate it , Take a look at when the program is in the foreground and background switching and horizontal and vertical screen switching , Will the timer reset ,
class TimerViewModel:ViewModel() {
private var timer:Timer?=null
private var currentSecond:Int = 0
fun startTiming(){
if (timer==null){
currentSecond = 0
timer = Timer()
val timerTask:TimerTask = MyTask()
timer?.schedule(timerTask,1000,1000)
}
}
private inner class MyTask:TimerTask(){
override fun run() {
currentSecond ++
if (onTimeChangeListener!=null){
onTimeChangeListener.onTimeChange(currentSecond)
}
}
}
interface OnTimeChangeListener{
fun onTimeChange(second:Int)
}
private lateinit var onTimeChangeListener:OnTimeChangeListener
fun setOnTimeChangeListener(onTimeChangeListener:OnTimeChangeListener){
this.onTimeChangeListener = onTimeChangeListener
}
/**
* When viewmodel No longer needed , That is, the related activity Be destroyed when the , This method is called by the system
* You can perform some resource release operations , But because of the rotation of the screen activity The reconstruction of does not trigger this method , Does not affect the viewmodel Life cycle of
*/
override fun onCleared() {
super.onCleared()
}
}
activity and viewmodel relation
private fun init() {
val timerViewModel = ViewModelProvider(this).get(TimerViewModel::class.java)
timerViewModel.setOnTimeChangeListener(object : TimerViewModel.OnTimeChangeListener {
override fun onTimeChange(second: Int) {
Log.e(TAG, "onTimeChange:$second ")
}
})
timerViewModel.startTiming()
}
It turns out that , stay activity When you are in the background and switching between horizontal and vertical screens ,viewmodel There's no change in the life cycle of

ViewModel And onSaveInstanceState() The difference between
onSaveInstanceState() Method can also solve the problem of data loss caused by screen rotation , however onSaveInstanceState() Method can only hold a small amount of , Data that supports serialization , and ViewModel There is no such restriction , It supports all the data in the page
When the page is completely destroyed ,ViewModel There is no data in , and onSaveInstanceState() There is no limit to the method .
LiveData Introduce
LiveData It's an observable data class container , Data can be packaged , Be an observed person , When the data changes, it can be captured by the observer .
LiveData Is an abstract class , We usually use subclasses of it MutableLiveData
LiveData Be able to sense the life cycle of a page , Check whether the current page is activated , Only when it is activated can it be notified , Otherwise it will be destroyed , binding activity The process is as follows
MutableLiveData usage
private var currentSecond:MutableLiveData<Int> = MutableLiveData()
private fun initComponent() {
val timerViewModel = ViewModelProvider(this).get(TimerViewModel::class.java)
var liveData = timerViewModel.getCurrentSecond()
liveData.observe(this, Observer {
Log.e(TAG, "initComponent::$it ")
})
}
边栏推荐
- I suddenly find that the request dependent package in NPM has been discarded. What should I do?
- Use of JMeter
- Hibernate learning 2 - lazy loading (delayed loading), dynamic SQL parameters, caching
- Analysis report on development trend and investment forecast of global and Chinese D-leucine industry from 2022 to 2028
- Global and Chinese tetrahydrofurfuryl butyrate industry operation pattern and future prospect report 2022 ~ 2028
- 怎么把wps表格里某一列有重复项的整行删掉
- 教程详解|在酷雷曼系统中如何编辑设置导览功能?
- JMeter socket connection sends data
- Go crawler framework -colly actual combat (IV) -- Zhihu answer crawl (I)
- 干接点和湿接点
猜你喜欢

Im instant messaging development application keeping alive process anti kill

Hibernate learning 3 - custom SQL

Color gradient gradient color collection

∞符号线条动画canvasjs特效

Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...

在滴滴和字节跳动干了 5年软件测试,太真实…

C# Winform 最大化遮挡任务栏和全屏显示问题

Unmanned driving: Some Thoughts on multi-sensor fusion

Signal integrity (SI) power integrity (PI) learning notes (XXV) differential pair and differential impedance (V)

5G dtu无线通信模块的电力应用
随机推荐
Signal integrity (SI) power integrity (PI) learning notes (I) introduction to signal integrity analysis
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
Use of JMeter
时间统一系统
Report on operation mode and future development trend of global and Chinese propenyl isovalerate industry from 2022 to 2028
Analysis report on operation trend and investment strategy of global and Chinese tetrahydrofurfuryl propionate industry from 2022 to 2028
Adding, deleting, modifying and checking in low build code
为什么越来越多的实体商铺用VR全景?优势有哪些?
What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis
wx小程序跳转页面
D omit parameter name
OTT营销之风正盛,商家到底该怎么投?
canvas线条的动态效果
节奏快?压力大?VR全景客栈带你体验安逸生活
【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学
Approaching harvest moon:moonbeam DFI Carnival
Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解
Hibernate learning 2 - lazy loading (delayed loading), dynamic SQL parameters, caching
JPA learning 2 - core annotation, annotation addition, deletion, modification and query, list query result return type, one to many, many to one, many to many

