
本文的解决方案主要集中在viewmodel的实现上。问题的核心在于,每次activity重建时,viewmodel中的livedata都会被重新观察,从而触发其内部的逻辑。为了避免这种情况,我们需要确保livedata只在第一次被观察时执行其初始化逻辑。
首先,我们来回顾一下原始的代码:
@HiltViewModel
public class MapViewModel extends ViewModel {
@NonNull
private final LocationRepository locationRepository;
@NonNull
private final NearBySearchRepository nearBySearchRepository;
@Inject
public MapViewModel(@NonNull LocationRepository locationRepository, @NonNull NearBySearchRepository nearBySearchRepository
) {
this.locationRepository = locationRepository;
this.nearBySearchRepository = nearBySearchRepository;
}
public SingleLiveEvent<LatLng> getFocusOnUser() {
SingleLiveEvent<LatLng> mediatorLiveData = new SingleLiveEvent<>();
LiveData<Location> locationLiveData = locationRepository.getLocationLiveData();
locationLiveData.getValue();
mediatorLiveData.addSource(locationLiveData, location -> {
if (location != null) {
mediatorLiveData.removeSource(locationLiveData);
mediatorLiveData.setValue(
new LatLng(location.getLatitude(), location.getLongitude())
);}
});
return mediatorLiveData;
}
}这段代码的问题在于,每次调用getFocusOnUser()时,都会创建一个新的SingleLiveEvent,并重新订阅locationLiveData。这导致每次Activity重建时,都会重新获取用户位置,并将地图重置到初始位置。
为了解决这个问题,我们可以将位置获取逻辑移至ViewModel的构造函数中,并使用SingleLiveEvent来确保只发送一次位置信息。
修改后的代码如下:
@HiltViewModel
public class MapViewModel extends ViewModel {
@NonNull
private final LocationRepository locationRepository;
@NonNull
private final NearBySearchRepository nearBySearchRepository;
SingleLiveEvent<LatLng> mediatorLiveData = new SingleLiveEvent<>();
MutableLiveData<Location> locationMutableLiveData = new MutableLiveData<>();
@Inject
public MapViewModel(@NonNull LocationRepository locationRepository, @NonNull NearBySearchRepository nearBySearchRepository
) {
this.locationRepository = locationRepository;
this.nearBySearchRepository = nearBySearchRepository;
LiveData<Location> locationLiveData = locationRepository.getLocationLiveData();
locationMutableLiveData.setValue(locationLiveData.getValue());
mediatorLiveData.addSource(locationLiveData, location -> {
if (location != null) {
mediatorLiveData.removeSource(locationLiveData);
mediatorLiveData.setValue(new LatLng(location.getLatitude(), location.getLongitude()));
}
});
}
public SingleLiveEvent<LatLng> getFocusOnUser() {
return mediatorLiveData;
}
}在这个修改后的代码中,我们将SingleLiveEvent的创建和locationLiveData的订阅移至ViewModel的构造函数中。这样,只有在ViewModel第一次被创建时,才会执行这些操作。之后,每次Activity重建时,都会使用同一个SingleLiveEvent实例,从而避免了重复获取位置信息。
此外,我们还使用了SingleLiveEvent来确保只发送一次位置信息。SingleLiveEvent是一个自定义的LiveData,它只允许观察者接收一次数据。这可以避免在Activity重建时,由于LiveData的特性而导致位置信息被多次发送。
总结
通过将位置获取逻辑移至ViewModel的构造函数中,并使用SingleLiveEvent来避免不必要的LiveData更新,我们可以有效地解决设备旋转导致LiveData被意外触发的问题,从而保持UI状态,提升用户体验。
注意事项
以上就是如何避免设备旋转时触发LiveData?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号