NT fragment: bind the view model to the activity
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 26 Nov 2019 22:00:39 +0000 (00:00 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 26 Nov 2019 22:00:39 +0000 (00:00 +0200)
the fragment can be re-created when navigating to the save progress
fragment. when it is re-created the view model instance is re-created too,
losing the unsaved transaction

app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java

index 7101f44..5b197b0 100644 (file)
@@ -86,13 +86,13 @@ public class NewTransactionFragment extends Fragment {
     @Override
     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
-        Activity activity = getActivity();
+        FragmentActivity activity = getActivity();
         if (activity == null)
             throw new RSInvalidStateException(
                     "getActivity() returned null within onActivityCreated()");
 
         list = activity.findViewById(R.id.new_transaction_accounts);
-        viewModel = ViewModelProviders.of(this)
+        viewModel = ViewModelProviders.of(activity)
                                       .get(NewTransactionModel.class);
         mProfile = Data.profile.getValue();
         listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);