From 3c207c28b4d75b37312b254e72f5cd34b2717c2e Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 28 Feb 2023 18:08:28 +0100 Subject: [PATCH] fix drawer layout reacting to back press after rotation --- .../database/AxolotlDatabaseStore.java | 2 +- .../ui/fragment/main/OverviewFragment.java | 33 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/im/conversations/android/database/AxolotlDatabaseStore.java b/app/src/main/java/im/conversations/android/database/AxolotlDatabaseStore.java index 60a8270ac..b31bffc00 100644 --- a/app/src/main/java/im/conversations/android/database/AxolotlDatabaseStore.java +++ b/app/src/main/java/im/conversations/android/database/AxolotlDatabaseStore.java @@ -38,7 +38,7 @@ public class AxolotlDatabaseStore extends AbstractAccountService implements Sign @Override public boolean saveIdentity( - final SignalProtocolAddress signalProtocolAddress, IdentityKey identityKey) { + final SignalProtocolAddress signalProtocolAddress, final IdentityKey identityKey) { final var address = AxolotlAddress.cast(signalProtocolAddress); final boolean isBTBVEnabled = true; return database.runInTransaction( diff --git a/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java b/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java index f6571fb16..b7425ebcc 100644 --- a/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java +++ b/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java @@ -36,6 +36,14 @@ public class OverviewFragment extends Fragment { private FragmentOverviewBinding binding; + final OnBackPressedCallback drawerLayoutOnBackPressedCallback = + new OnBackPressedCallback(false) { + @Override + public void handleOnBackPressed() { + binding.drawerLayout.close(); + } + }; + private OverviewViewModel overviewViewModel; @Override @@ -77,31 +85,32 @@ public class OverviewFragment extends Fragment { return binding.getRoot(); } + @Override + public void onResume() { + super.onResume(); + // after rotation (or similar) the drawer layout might get opened in restoreInstanceState + // therefor we need to check again if we need to enable the callback + this.drawerLayoutOnBackPressedCallback.setEnabled(this.binding.drawerLayout.isOpen()); + } + private void configureDrawerLayoutToCloseOnBackPress() { - final OnBackPressedCallback onBackPressedCallback = - new OnBackPressedCallback(false) { - @Override - public void handleOnBackPressed() { - binding.drawerLayout.close(); - } - }; this.binding.drawerLayout.addDrawerListener( new DrawerLayout.SimpleDrawerListener() { @Override - public void onDrawerOpened(View drawerView) { + public void onDrawerOpened(final View drawerView) { super.onDrawerOpened(drawerView); - onBackPressedCallback.setEnabled(true); + drawerLayoutOnBackPressedCallback.setEnabled(true); } @Override - public void onDrawerClosed(View drawerView) { + public void onDrawerClosed(final View drawerView) { super.onDrawerClosed(drawerView); - onBackPressedCallback.setEnabled(false); + drawerLayoutOnBackPressedCallback.setEnabled(false); } }); requireActivity() .getOnBackPressedDispatcher() - .addCallback(getViewLifecycleOwner(), onBackPressedCallback); + .addCallback(getViewLifecycleOwner(), this.drawerLayoutOnBackPressedCallback); } private boolean onNavigationItemSelected(final MenuItem menuItem) {