fix drawer layout reacting to back press after rotation

This commit is contained in:
Daniel Gultsch 2023-02-28 18:08:28 +01:00
parent 9c95554782
commit 3c207c28b4
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
2 changed files with 22 additions and 13 deletions

View file

@ -38,7 +38,7 @@ public class AxolotlDatabaseStore extends AbstractAccountService implements Sign
@Override @Override
public boolean saveIdentity( public boolean saveIdentity(
final SignalProtocolAddress signalProtocolAddress, IdentityKey identityKey) { final SignalProtocolAddress signalProtocolAddress, final IdentityKey identityKey) {
final var address = AxolotlAddress.cast(signalProtocolAddress); final var address = AxolotlAddress.cast(signalProtocolAddress);
final boolean isBTBVEnabled = true; final boolean isBTBVEnabled = true;
return database.runInTransaction( return database.runInTransaction(

View file

@ -36,6 +36,14 @@ public class OverviewFragment extends Fragment {
private FragmentOverviewBinding binding; private FragmentOverviewBinding binding;
final OnBackPressedCallback drawerLayoutOnBackPressedCallback =
new OnBackPressedCallback(false) {
@Override
public void handleOnBackPressed() {
binding.drawerLayout.close();
}
};
private OverviewViewModel overviewViewModel; private OverviewViewModel overviewViewModel;
@Override @Override
@ -77,31 +85,32 @@ public class OverviewFragment extends Fragment {
return binding.getRoot(); return binding.getRoot();
} }
private void configureDrawerLayoutToCloseOnBackPress() {
final OnBackPressedCallback onBackPressedCallback =
new OnBackPressedCallback(false) {
@Override @Override
public void handleOnBackPressed() { public void onResume() {
binding.drawerLayout.close(); 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() {
this.binding.drawerLayout.addDrawerListener( this.binding.drawerLayout.addDrawerListener(
new DrawerLayout.SimpleDrawerListener() { new DrawerLayout.SimpleDrawerListener() {
@Override @Override
public void onDrawerOpened(View drawerView) { public void onDrawerOpened(final View drawerView) {
super.onDrawerOpened(drawerView); super.onDrawerOpened(drawerView);
onBackPressedCallback.setEnabled(true); drawerLayoutOnBackPressedCallback.setEnabled(true);
} }
@Override @Override
public void onDrawerClosed(View drawerView) { public void onDrawerClosed(final View drawerView) {
super.onDrawerClosed(drawerView); super.onDrawerClosed(drawerView);
onBackPressedCallback.setEnabled(false); drawerLayoutOnBackPressedCallback.setEnabled(false);
} }
}); });
requireActivity() requireActivity()
.getOnBackPressedDispatcher() .getOnBackPressedDispatcher()
.addCallback(getViewLifecycleOwner(), onBackPressedCallback); .addCallback(getViewLifecycleOwner(), this.drawerLayoutOnBackPressedCallback);
} }
private boolean onNavigationItemSelected(final MenuItem menuItem) { private boolean onNavigationItemSelected(final MenuItem menuItem) {