From 33b9d81eee0efad2fe1a277d5e380589b28d00a8 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 22 Dec 2019 09:13:32 +0200 Subject: [PATCH] use getViewLifecycleOwner when observing LiveData from fragments --- .../ui/account_summary/AccountSummaryFragment.java | 5 +++-- .../mobileledger/ui/activity/NewTransactionFragment.java | 4 ++-- .../ui/transaction_list/TransactionListFragment.java | 7 ++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index bb484929..6482dfd1 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -54,7 +54,8 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { debug("flow", "AccountSummaryFragment.onCreate()"); setHasOptionsMenu(true); - Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged); + Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(), + this::onBackgroundTaskRunningChanged); } public void onAttach(@NotNull Context context) { super.onAttach(context); @@ -93,7 +94,7 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { manageFabOnScroll(); swiper = mActivity.findViewById(R.id.account_swiper); - Colors.themeWatch.observe(this, this::themeChanged); + Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged); swiper.setOnRefreshListener(() -> { debug("ui", "refreshing accounts via swipe"); Data.scheduleTransactionListRetrieval(mActivity); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java index 5cbbab71..54c55fa5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java @@ -98,7 +98,7 @@ public class NewTransactionFragment extends Fragment { listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile); list.setAdapter(listAdapter); list.setLayoutManager(new LinearLayoutManager(activity)); - Data.profile.observe(this, profile -> { + Data.profile.observe(getViewLifecycleOwner(), profile -> { mProfile = profile; listAdapter.setProfile(profile); }); @@ -142,7 +142,7 @@ public class NewTransactionFragment extends Fragment { }).attachToRecyclerView(list); viewModel.isSubmittable() - .observe(this, isSubmittable -> { + .observe(getViewLifecycleOwner(), isSubmittable -> { if (isSubmittable) { if (fab != null) { fab.show(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java index 8de45dce..d71c0b41 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java @@ -56,7 +56,8 @@ public class TransactionListFragment extends MobileLedgerListFragment { public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); - Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged); + Data.backgroundTasksRunning.observe(getViewLifecycleOwner(), + this::onBackgroundTaskRunningChanged); } @Override public void onAttach(@NotNull Context context) { @@ -111,7 +112,7 @@ public class TransactionListFragment extends MobileLedgerListFragment { Data.scheduleTransactionListRetrieval(mActivity); }); - Colors.themeWatch.observe(this, this::themeChanged); + Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged); vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter); accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name); @@ -124,7 +125,7 @@ public class TransactionListFragment extends MobileLedgerListFragment { Globals.hideSoftKeyboard(mActivity); }); - Data.accountFilter.observe(this, this::onAccountNameFilterChanged); + Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged); TransactionListViewModel.updating.addObserver( (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get())); -- 2.39.5