Aug*_*rmo 15 android viewmodel android-viewmodel android-architecture-components
我正在学习ViewModel,LiveData并且在此过程中产生了疑问。
如果我需要启动一个,我应该怎么做Activity?
是否可以将上下文作为参数传递给ViewModel(上下文不会存储在 ViewModel 中)?
ActivityAViewModel : ViewModel() {
// ...
fun openActivityB(context: Context) {
context.startActivity(...)
}
// ...
}
ActivityA {
// ...
fun onSomethingHappened() {
viewModel.openActivityB(this)
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
如果不是,在这种情况下最正确的做法是什么?
小智 10
您应该从活动中调用 startActivity,而不是从视图模型中调用。如果你想从 viewmodel 打开它,你需要在 viewmodel 中创建带有一些导航参数的 livedata 并观察活动中的 livedata。
我喜欢触发事件。:D
正如大家所说,ViewModel不应包含Context或引用包含Context. 所以startActivity从ViewModel.
我要做的是有一个包含事件数据的 LiveData。此事件将根据您的业务逻辑从您的 ViewModel 触发(也许您正在显示一个倒计时,并在它结束时移动到下一个活动?)。它是一个LiveData,你可以在上面观察。根据此事件的数据,您可以开始您的活动。
你可能想看看SingleLiveEvent
小智 5
恕我直言,viewmodel 应该对视图以及它如何向用户呈现信息一无所知。
/**
* Activity (as view) responsible only for gathering actions and intentions from user and
* show result state.
* View must know "What user want". View knows meaning its interface.
* Click on button 'login' means INTENTION to login somewhere.
* This intention pass to ViewModel to process it and wait some changing state from LiveData.
* For example implemented as Actions.
*/
public class LoginActivity extends AppCompatActivity {
private LoginViewModel mLoginViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLoginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
mLoginViewModel.getAction().observe(this, new Observer<Action>() {
@Override
public void onChanged(@Nullable final Action action) {
if(action != null){
handleAction(action);
}
}
});
//Emulate user intention
mLoginViewModel.userWantToLogin("0123456789", "admin");
}
private void handleAction(@NonNull final Action action) {
switch (action.getValue()){
case Action.SHOW_WELCOME:
//show Activity.
break;
case Action.SHOW_INVALID_PASSWARD_OR_LOGIN:
//show Toast
break;
}
}
}
public class LoginViewModel extends ViewModel {
//Stores actions for view.
private MutableLiveData<Action> mAction = new MutableLiveData<>();
public LiveData<Action> getAction() {
return mAction;
}
/**
* Takes intention to login from user and process it.
*
* @param password Dummy password.
* @param login Dummy login.
*/
public void userWantToLogin(String password, String login){
if(validateInfo(password, login)){
showWelcomeScreen();
}else {
showPasswordOrLoginInvalid();
}
}
/*
* Changes LiveData. Does not act directly with view.
* View can implement any way to show info
* to user (show new activity, alert or toast)
*/
private void showPasswordOrLoginInvalid() {
mAction.setValue(new Action(Action.SHOW_INVALID_PASSWARD_OR_LOGIN));
}
/*
* Changes LiveData. Does not act directly with view.
* View can implement any way to show info
* to user (show new activity, alert or toast)
*/
private void showWelcomeScreen() {
mAction.setValue(new Action(Action.SHOW_WELCOME));
}
//As example of some logic.
private boolean validateInfo(String password, String login) {
return password.equals("0123456789") && login.equals("admin");
}
}
public class Action {
public static final int SHOW_WELCOME = 0;
public static final int SHOW_INVALID_PASSWARD_OR_LOGIN = 1;
private final int mAction;
public Action(int action) {
mAction = action;
}
public int getValue() {
return mAction;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11518 次 |
| 最近记录: |