From 26f1c43b0fd7a13b0d0d88845dcf8d036b413934 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 30 Jan 2021 15:11:11 +0200 Subject: [PATCH] move model instantiation from onAttach to onCreateView parent activity may not be fully up in onAttach() and return a null navigation controller, resulting in a crash upon reconfiguration --- .../ui/patterns/PatternDetailsFragment.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternDetailsFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternDetailsFragment.java index 2c262105..8b5ed04b 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternDetailsFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternDetailsFragment.java @@ -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) { -- 2.39.5