]> git.ktnx.net Git - mobile-ledger.git/commitdiff
move model instantiation from onAttach to onCreateView
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 30 Jan 2021 13:11:11 +0000 (15:11 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 30 Jan 2021 13:11:11 +0000 (15:11 +0200)
parent activity may not be fully up in onAttach() and return a null
navigation controller, resulting in a crash upon reconfiguration

app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternDetailsFragment.java

index 2c2621055d5a44408d191ee62b41e46aed033ace..8b5ed04b47b0e7b3fab74c3b73cd234f4dafac86 100644 (file)
@@ -71,6 +71,13 @@ public class PatternDetailsFragment extends QRScanAbleFragment {
     @Override
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                              @Nullable Bundle savedInstanceState) {
+        NavController controller = ((PatternsActivity) requireActivity()).getNavController();
+        final ViewModelStoreOwner viewModelStoreOwner =
+                controller.getViewModelStoreOwner(R.id.pattern_list_navigation);
+        mViewModel = new ViewModelProvider(viewModelStoreOwner).get(PatternDetailsViewModel.class);
+        mViewModel.setDefaultPatternName(getString(R.string.unnamed_pattern));
+        Logger.debug("flow", "PatternDetailsFragment.onCreateView(): model=" + mViewModel);
+
         b = PatternDetailsFragmentBinding.inflate(inflater);
         Context context = b.patternDetailsRecyclerView.getContext();
         if (mColumnCount <= 1) {
@@ -86,18 +93,8 @@ public class PatternDetailsFragment extends QRScanAbleFragment {
         b.patternDetailsRecyclerView.setAdapter(adapter);
         mViewModel.getItems(mPatternId)
                   .observe(getViewLifecycleOwner(), adapter::setItems);
-        return b.getRoot();
-    }
-    @Override
-    public void onAttach(@NonNull Context context) {
-        super.onAttach(context);
-        NavController controller = ((PatternsActivity) context).getNavController();
-        final ViewModelStoreOwner viewModelStoreOwner =
-                controller.getViewModelStoreOwner(R.id.pattern_list_navigation);
-        mViewModel = new ViewModelProvider(viewModelStoreOwner).get(PatternDetailsViewModel.class);
-        mViewModel.setDefaultPatternName(getString(R.string.unnamed_pattern));
-        Logger.debug("flow", "PatternDetailsFragment.onAttach(): model=" + mViewModel);
 
+        return b.getRoot();
     }
     @Override
     protected void onQrScanned(String text) {