Skip to content
Henry Tao edited this page Apr 24, 2016 · 4 revisions

Activity

public class TaskDetailActivity extends MVVMActivity {

  private TaskDetailActivityBinding mBinding;

  private TaskDetailViewModel mViewModel;

  @Override
  public void onInitializeViewModels() {
    mViewModel = new TaskDetailViewModel(taskId);
    addViewModel(mViewModel);
  }

  @Override
  public void onSetContentView(Bundle savedInstanceState) {
    mBinding = DataBindingUtil.setContentView(this, R.layout.task_detail_activity);
    mBinding.setViewModel(mViewModel);

    setSupportActionBar(mBinding.toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mBinding.toolbar.setNavigationOnClickListener(v -> onBackPressed());
    ...
  }
}

Fragment

public class TasksFragment extends MVVMFragment {

  private TasksFragmentBinding mBinding;

  private TasksViewModel mViewModel;

  @Override
  public void onCreateView() {
    super.onCreateView();
    setHasOptionsMenu(true);
    ...
  }

  @Override
  public View onInflateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mBinding = DataBindingUtil.inflate(inflater, R.layout.tasks_fragment, container, false);
    return mBinding.getRoot();
  }

  @Override
  public void onInitializeViewModels() {
    mViewModel = new TasksViewModel();
    addViewModel(mViewModel);
  }
}

ViewModel

public class TasksViewModel extends MVVMViewModel {

  public TasksViewModel() {
    ...
  }
}