Skip to content

Commit

Permalink
Start migration to getOnBackPressedDispatcher and restore back button
Browse files Browse the repository at this point in the history
The back button now works as it prior to the change, there is a setting to configure if you want the back button to open the feeds list or to just exit (default)
  • Loading branch information
mentalinc committed Oct 15, 2023
1 parent e4cdb67 commit 1e16613
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ public void updateActionBarIcons() {

@Override
public void onBackPressed() {
Log.v(TAG,"NewsDetailActivity onBackPressed() Called");
if (!handlePodcastBackPressed())
super.onBackPressed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import android.widget.SearchView;
import android.widget.Toast;

import androidx.activity.OnBackPressedCallback;
import androidx.activity.OnBackPressedDispatcher;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.ActionBarDrawerToggle;
Expand Down Expand Up @@ -209,6 +211,33 @@ protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);


//trying to get back button working again
OnBackPressedDispatcher dispatcher = getOnBackPressedDispatcher();
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Log.d(TAG, "NewsReaderListActivity handleOnBackPressed() called");
if (!handlePodcastBackPressed()) {

//Add in check for preference to determine if closes app or opens drawer
if (mPrefs.getBoolean(SettingsActivity.CB_PREF_BACK_OPENS_DRAWER, false)) { //change this line to the preference test - or add into an AND in the below iff

if (binding.drawerLayout != null) {
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START))
setEnabled(false);
else
binding.drawerLayout.openDrawer(GravityCompat.START);
} else {
setEnabled(false);
}
} else {
setEnabled(false);
}
}
}
});

binding = ActivityNewsreaderBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

Expand Down Expand Up @@ -859,28 +888,30 @@ public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}

// remove this method overide as no longer works when using the new android predictive back gestures
// cleanup once confirmed new approach is working as expected
/*
@Override
public void onBackPressed() {
Log.e(TAG,"NewsReaderListActivity onBackPressed() Called");
if (!handlePodcastBackPressed()) {
//Add in check for preference to determine if closes app or opens drawer
Boolean backAction = mPrefs.getBoolean(SettingsActivity.CB_PREF_BACK_OPENS_DRAWER,false);
//if (backAction) { //change this line to the preference test - or add into an AND in the below iff
if (backAction) { //change this line to the preference test - or add into an AND in the below iff
if (mPrefs.getBoolean(SettingsActivity.CB_PREF_BACK_OPENS_DRAWER,false)) { //change this line to the preference test - or add into an AND in the below iff
if (binding.drawerLayout != null) {
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START))
super.onBackPressed();
else
//super.onBackPressed();
//else
binding.drawerLayout.openDrawer(GravityCompat.START);
} else {
super.onBackPressed();
//super.onBackPressed();
}
}else {
super.onBackPressed();
}
}
}

*/
public static final int RESULT_SETTINGS = 15642;

private void syncMenuItemUnreadOnly() {
Expand Down
1 change: 0 additions & 1 deletion News-Android-App/src/main/res/xml/pref_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
android:title="@string/pref_title_BackButtonOpensDrawer"
app:iconSpaceReserved="false" />


<ListPreference
android:defaultValue="1"
android:entries="@array/pref_general_swipe_action"
Expand Down

0 comments on commit 1e16613

Please sign in to comment.