From c3f5273813eaa0f3778d3ff158790e0893e37c14 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 28 Feb 2023 06:41:33 +0100 Subject: [PATCH] close drawer on back press --- .../ui/fragment/main/OverviewFragment.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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 2e5bb4811..f6571fb16 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 @@ -7,9 +7,11 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; +import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import com.google.android.material.elevation.SurfaceColors; @@ -71,9 +73,37 @@ public class OverviewFragment extends Fragment { this.overviewViewModel .getChatFilterAvailable() .observe(getViewLifecycleOwner(), this::onChatFilterAvailable); + this.configureDrawerLayoutToCloseOnBackPress(); return binding.getRoot(); } + 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) { + super.onDrawerOpened(drawerView); + onBackPressedCallback.setEnabled(true); + } + + @Override + public void onDrawerClosed(View drawerView) { + super.onDrawerClosed(drawerView); + onBackPressedCallback.setEnabled(false); + } + }); + requireActivity() + .getOnBackPressedDispatcher() + .addCallback(getViewLifecycleOwner(), onBackPressedCallback); + } + private boolean onNavigationItemSelected(final MenuItem menuItem) { if (menuItem.getItemId() == R.id.chats) { setChatFilter(null);