Skip to content

Commit

Permalink
Rework on back pressed to support API 33 and above
Browse files Browse the repository at this point in the history
Refactor method to configure on back pressed dispatcher.
Use on back pressed callbacks for search view, bottom sheet and
navigation drawer.
  • Loading branch information
pranavpandey committed Dec 1, 2022
1 parent 89a743e commit 1e55104
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
Expand Down Expand Up @@ -200,6 +201,44 @@ public abstract class DynamicActivity extends DynamicStateActivity
*/
protected boolean mAppBarVisible;

/**
* On back pressed callback to collapse the search view.
*/
protected final OnBackPressedCallback mSearchViewOnBackPressedCallback =
new OnBackPressedCallback(false) {
@Override
public void handleOnBackPressed() {
onBackPressed();
}
};

/**
* On back pressed callback to expand the bottom sheet.
*/
protected final OnBackPressedCallback mBottomSheetOnBackPressedCallback =
new OnBackPressedCallback(false) {
@Override
public void handleOnBackPressed() {
onBackPressed();
}
};

/**
* The bottom sheet callback to enable on back pressed callback accordingly.
*/
protected final BottomSheetBehavior.BottomSheetCallback mBottomSheetCallback =
new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet,
@BottomSheetBehavior.State int newState) {
mBottomSheetOnBackPressedCallback.setEnabled(
newState == BottomSheetBehavior.STATE_COLLAPSED);
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) { }
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -282,6 +321,14 @@ public void onClick(View view) {
}
}

@Override
protected void onConfigureOnBackPressedDispatcher() {
super.onConfigureOnBackPressedDispatcher();

getOnBackPressedDispatcher().addCallback(mSearchViewOnBackPressedCallback);
getOnBackPressedDispatcher().addCallback(mBottomSheetOnBackPressedCallback);
}

@Override
protected void onAdjustElevation() {
super.onAdjustElevation();
Expand Down Expand Up @@ -950,6 +997,13 @@ public void setBottomBarShadowVisible(boolean visible) {
*/
public void addBottomSheet(@Nullable View view, boolean removePrevious) {
addView(mBottomSheet, view, removePrevious);

if (isExpandBottomSheetOnExit() && getBottomSheetBehavior() != null) {
getBottomSheetBehavior().addBottomSheetCallback(mBottomSheetCallback);

mBottomSheetOnBackPressedCallback.setEnabled(getBottomSheetBehavior().getState()
== BottomSheetBehavior.STATE_COLLAPSED);
}
}

/**
Expand Down Expand Up @@ -1319,6 +1373,8 @@ public void setSearchViewListener(@Nullable DynamicSearchListener dynamicSearchL

@Override
public void onSearchViewExpanded() {
mSearchViewOnBackPressedCallback.setEnabled(true);

if (!(this instanceof DynamicDrawerActivity)) {
setNavigationIcon(R.drawable.ads_ic_back);
}
Expand All @@ -1330,6 +1386,8 @@ public void onSearchViewExpanded() {

@Override
public void onSearchViewCollapsed() {
mSearchViewOnBackPressedCallback.setEnabled(false);

if (!(this instanceof DynamicDrawerActivity)) {
setNavigationIcon(getDefaultNavigationIcon());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
Expand Down Expand Up @@ -87,6 +88,17 @@ public abstract class DynamicDrawerActivity extends DynamicActivity
*/
private TextView mNavHeaderSubtitle;

/**
* On back pressed callback to close the drawers.
*/
protected final OnBackPressedCallback mDrawerOnBackPressedCallback =
new OnBackPressedCallback(false) {
@Override
public void handleOnBackPressed() {
onBackPressed();
}
};

@Override
protected @LayoutRes int getLayoutRes() {
return setCollapsingToolbarLayout() ? R.layout.ads_activity_drawer_collapsing
Expand Down Expand Up @@ -119,6 +131,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setNavigationBarColor(getNavigationBarColor());
}

@Override
protected void onConfigureOnBackPressedDispatcher() {
super.onConfigureOnBackPressedDispatcher();

getOnBackPressedDispatcher().addCallback(mDrawerOnBackPressedCallback);
}

@Override
public void onResume() {
super.onResume();
Expand Down Expand Up @@ -196,10 +215,14 @@ this, mDrawer, getToolbar(), R.string.ads_navigation_drawer_open,
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) { }

@Override
public void onDrawerOpened(@NonNull View drawerView) { }
public void onDrawerOpened(@NonNull View drawerView) {
mDrawerOnBackPressedCallback.setEnabled(!isPersistentDrawer());
}

@Override
public void onDrawerClosed(@NonNull View drawerView) { }
public void onDrawerClosed(@NonNull View drawerView) {
mDrawerOnBackPressedCallback.setEnabled(false);
}

@Override
public void onDrawerStateChanged(int newState) {
Expand All @@ -221,6 +244,8 @@ private void configureDrawer() {
return;
}

mDrawerOnBackPressedCallback.setEnabled(!isPersistentDrawer() && isDrawerOpen());

if (isPersistentDrawer()) {
setNavigationShadowVisible(true);

Expand Down Expand Up @@ -262,18 +287,24 @@ public void onClick(View v) {
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) { }

@Override
public void onDrawerOpened(@NonNull View drawerView) { }
public void onDrawerOpened(@NonNull View drawerView) {
mDrawerOnBackPressedCallback.setEnabled(true);
}

@Override
public void onDrawerClosed(@NonNull View drawerView) {
mDrawerOnBackPressedCallback.setEnabled(false);

if (mNavigationItemSelected) {
mNavigationItemSelected = false;
onNavigationItemSelected(mNavigationItemId, true);
}
}

@Override
public void onDrawerStateChanged(int newState) { }
public void onDrawerStateChanged(int newState) {
collapseSearchView();
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public Context createConfigurationContext(Configuration overrideConfiguration) {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
onRegisterOnBackInvokedDispatcher();
onConfigureOnBackPressedDispatcher();
updateThemeFromIntent(getIntent());
setDynamicTheme();
onSetSharedElementTransition();
Expand Down Expand Up @@ -315,10 +315,11 @@ protected void onNewIntent(Intent intent) {
}

/**
* This method will be called to register on back invoked dispatcher on API 33 and above.
* This method will be called to configure on back pressed callback to support
* API 33 and above.
*/
@TargetApi(Build.VERSION_CODES.TIRAMISU)
protected void onRegisterOnBackInvokedDispatcher() { }
protected void onConfigureOnBackPressedDispatcher() { }

/**
* Setup content according to the intent.
Expand Down Expand Up @@ -1782,11 +1783,7 @@ protected void updateTaskDescription(@ColorInt int color) {

@Override
public void onBackPressed() {
if (getOnBackPressedDispatcher().hasEnabledCallbacks()) {
getOnBackPressedDispatcher().onBackPressed();
} else {
finishActivity();
}
finishActivity();
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
-->

<manifest
xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<queries>
<intent>
Expand All @@ -32,7 +33,9 @@
android:label="@string/app_name"
android:theme="@style/Sample"
android:supportsRtl="true"
android:allowBackup="false">
android:enableOnBackInvokedCallback="true"
tools:ignore="RtlEnabled"
tools:targetApi="TIRAMISU">

<meta-data android:name="android.max_aspect" android:value="2.1" />

Expand Down

0 comments on commit 1e55104

Please sign in to comment.