Android:MVVM是否可以显示来自ViewModel的消息(toast/snackbar等)
2022-09-03 08:51:44
我想知道在ViewModel的视图中显示某种消息的最佳方法是什么。我的视图模型正在进行POST调用和“onResult”我想弹出一条消息给用户,其中包含某个消息。
这是我的视图模型:
public class RegisterViewModel extends ViewModel implements Observable {
.
.
.
public void registerUser(PostUserRegDao postUserRegDao) {
repository.executeRegistration(postUserRegDao).enqueue(new Callback<RegistratedUserDTO>() {
@Override
public void onResponse(Call<RegistratedUserDTO> call, Response<RegistratedUserDTO> response) {
RegistratedUserDTO registratedUserDTO = response.body();
/// here I want to set the message and send it to the Activity
if (registratedUserDTO.getRegisterUserResultDTO().getError() != null) {
}
}
});
}
我的活动:
public class RegisterActivity extends BaseActivity {
@Override
protected int layoutRes() {
return R.layout.activity_register;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
ActivityRegisterBinding binding = DataBindingUtil.setContentView(this, layoutRes());
binding.setViewModel(mRegisterViewModel);
}
在这种情况下,最好的方法是什么?