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
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(

View file

@ -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();
}
private void configureDrawerLayoutToCloseOnBackPress() {
final OnBackPressedCallback onBackPressedCallback =
new OnBackPressedCallback(false) {
@Override
public void handleOnBackPressed() {
binding.drawerLayout.close();
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() {
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) {