
本文的解决方案主要集中在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 getFocusOnUser() {
SingleLiveEvent mediatorLiveData = new SingleLiveEvent<>();
LiveData 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 mediatorLiveData = new SingleLiveEvent<>();
MutableLiveData locationMutableLiveData = new MutableLiveData<>();
@Inject
public MapViewModel(@NonNull LocationRepository locationRepository, @NonNull NearBySearchRepository nearBySearchRepository
) {
this.locationRepository = locationRepository;
this.nearBySearchRepository = nearBySearchRepository;
LiveData 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 getFocusOnUser() {
return mediatorLiveData;
}
} 在这个修改后的代码中,我们将SingleLiveEvent的创建和locationLiveData的订阅移至ViewModel的构造函数中。这样,只有在ViewModel第一次被创建时,才会执行这些操作。之后,每次Activity重建时,都会使用同一个SingleLiveEvent实例,从而避免了重复获取位置信息。
此外,我们还使用了SingleLiveEvent来确保只发送一次位置信息。SingleLiveEvent是一个自定义的LiveData,它只允许观察者接收一次数据。这可以避免在Activity重建时,由于LiveData的特性而导致位置信息被多次发送。
总结
通过将位置获取逻辑移至ViewModel的构造函数中,并使用SingleLiveEvent来避免不必要的LiveData更新,我们可以有效地解决设备旋转导致LiveData被意外触发的问题,从而保持UI状态,提升用户体验。
注意事项
- 确保你的ViewModel是@HiltViewModel注解的,并且使用了Hilt进行依赖注入。
- SingleLiveEvent需要自定义实现,可以参考Google Samples中的实现。
- 根据你的具体需求,可能需要对代码进行适当的调整。









