我遇到了有关屏幕旋转期间 ViewModel 中 LiveData 行为的问题。
我的应用程序围绕 MVVM 模式构建,包括一个 Activity,它从 ViewModel 观察 LiveData,而 ViewModel 又从链接到 Room 数据库的存储库中获取数据。
以下是涉及的代码片段:
Create 上的活动:
ChatViewModel2.getInstance(this, topic).getTopicChatLiveData().observe(this, chatMessageDBS -> {
chatAdapter.submitList(chatMessageDBS.stream().distinct().map(ChatMessageViewModel::createInstance).collect(Collectors.toList()));
});
视图模型:
public class ChatViewModel2 extends ViewModel {
private final ChatMassagesRepository repository;
private String topic;
private static ChatViewModel2 instance;
public ChatViewModel2(@NonNull String topic) {
repository = new ChatMassagesRepository();
this.topic = topic;
}
public static synchronized ChatViewModel2 getInstance(Context context, String topic) {
if (instance == null) {
instance = new ViewModelProvider((ViewModelStoreOwner) context, new ChatMassagesViewModelFactory(topic)).get(ChatViewModel2.class);
}
return instance;
}
public LiveData<List<ChatMessageDB>> getTopicChatLiveData() {
LiveData<List<ChatMessageDB>> topicChatLiveData = repository.getAllRepliesByTopic(topic);
return topicChatLiveData;
}
}
视图模型工厂:
public class ChatMassagesViewModelFactory implements ViewModelProvider.Factory {
private String topic;
public ChatMassagesViewModelFactory(String topic) {
this.topic = topic;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass.isAssignableFrom(ChatViewModel2.class)) {
return (T) new ChatViewModel2(topic);
}
throw new IllegalArgumentException("Unknown ViewModel class");
}
}
存储库:
public class ChatMassagesRepository {
ChatMessageDAO chatMessageDao;
private static ChatMassagesRepository INSTANCE;
public static ChatMassagesRepository getInstance() {
if (INSTANCE == null) {
INSTANCE = new ChatMassagesRepository();
}
return INSTANCE;
}
public ChatMassagesRepository() {
chatMessageDao= ChatDatabase.getInstance().chatMessageDao();
}
public LiveData<List<ChatMessageDB>> getAllRepliesByTopic(String topic){
LiveData<List<ChatMessageDB>> replies = chatMessageDao.getTopicChat(topic);
return replies;
}
}
这是来自数据库类的 getInstance:
@NonNull
public static ChatDatabase getInstance(final Context context){
synchronized (ChatDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), ChatDatabase.class, "teleconsole_chat_database").fallbackToDestructiveMigration().build();
}
}
return INSTANCE;
}
public static ChatDatabase getInstance(){
return getInstance(AppController.getInstance());
}
一切正常,直到屏幕旋转。旋转后,活动中的观察者收到一个空列表。如果您能深入了解为什么会发生这种情况以及如何纠正它,我们将不胜感激。
我也尝试过
new ViewModelProvider(this, new ChatMassagesViewModelFactory(topic)).get(ChatViewModel2.class).getTopicChatLiveData().observe(this, chatMessageDBS ->
chatAdapter.submitList(chatMessageDBS.stream().distinct().map(ChatMessageViewModel::createInstance).collect(Collectors.toList())));
但没有帮助
您可以尝试在清单文件中使用 `android:"configChanges:orientation" 来忽略屏幕旋转。但这不是最好的方法。 文档链接:https://developer.android.com/guide/topics/resources/runtime-changes#restrict-activity
如果解决方案有帮助,请告诉我。