提交 62a95286 编写于 作者: I Ian Lake

Prefer onViewCreated() over onActivityCreated()

The stated purpose of onActivityCreated() is to wait until the activity is created. That isn't needed here, where what we're actually doing is setting up the binding with the ViewModel, making this more suitable for onViewCreated(). As onActivityCreated() and onViewCreated() happen back to back, there's no actual difference in behavior (just increased clarity on what we're doing).
上级 d305c9c8
......@@ -57,9 +57,7 @@ public class ProductFragment extends Fragment {
};
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
ProductViewModel.Factory factory = new ProductViewModel.Factory(
requireActivity().getApplication(), requireArguments().getInt(KEY_PRODUCT_ID));
......
......@@ -58,8 +58,8 @@ public class ProductListFragment extends Fragment {
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final ProductListViewModel viewModel =
new ViewModelProvider(this).get(ProductListViewModel.class);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册