1
0
Fork 0

got rid of the the deprecated PreferencesActivity.onBackPressed()

This commit is contained in:
sspanak 2025-02-18 13:36:23 +02:00 committed by Dimo Karaivanov
parent c28c9f053e
commit 54d70c8cc2
4 changed files with 34 additions and 12 deletions

View file

@ -15,6 +15,7 @@
</queries> </queries>
<application <application
android:enableOnBackInvokedCallback="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name" android:label="@string/app_name"

View file

@ -3,6 +3,7 @@ package io.github.sspanak.tt9.preferences;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
@ -52,6 +53,7 @@ public class PreferencesActivity extends ActivityWithNavigation implements Prefe
validateFunctionKeys(); validateFunctionKeys();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setOnBackPressed();
// changing the theme causes onCreate(), which displays the MainSettingsScreen, // changing the theme causes onCreate(), which displays the MainSettingsScreen,
// but leaves the old "back" history, which is no longer valid, // but leaves the old "back" history, which is no longer valid,
@ -98,20 +100,26 @@ public class PreferencesActivity extends ActivityWithNavigation implements Prefe
} }
@Override /**
public void onBackPressed() { * onBackPressed() is deprecated, so calling the onBackPressed() on the Fragments is now more complicated.
Fragment previousFragment = getSupportFragmentManager().findFragmentById(R.id.preferences_container); */
if (previousFragment instanceof BaseScreenFragment) { private void setOnBackPressed() {
((BaseScreenFragment) previousFragment).onBackPressed(); OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {
} @Override
public void handleOnBackPressed() {
Fragment previousFragment = getSupportFragmentManager().findFragmentById(R.id.preferences_container);
if (previousFragment instanceof BaseScreenFragment) {
((BaseScreenFragment) previousFragment).onBackPressed();
}
super.onBackPressed(); setEnabled(false);
getOnBackPressedDispatcher().onBackPressed();
setEnabled(true);
}
};
Fragment nextFragment = getSupportFragmentManager().findFragmentById(R.id.preferences_container); // On API >= 33, this requires android:enableOnBackInvokedCallback="true" in the manifest
if (nextFragment instanceof BaseScreenFragment) { getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
((BaseScreenFragment) nextFragment).onBackPressed();
getOptionsCount = ((BaseScreenFragment) nextFragment)::getPreferenceCount;
}
} }

View file

@ -66,6 +66,7 @@ abstract public class BaseScreenFragment extends PreferenceFragmentCompat {
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
setScreenTitle(); setScreenTitle();
setActivityOptionCounter();
} }
@ -86,6 +87,13 @@ abstract public class BaseScreenFragment extends PreferenceFragmentCompat {
} }
private void setActivityOptionCounter() {
if (activity != null) {
activity.setOptionsCount(this::getPreferenceCount);
}
}
public void resetFontSize(boolean reloadList) { public void resetFontSize(boolean reloadList) {
initPreferencesList(); initPreferencesList();
preferencesList.getAll(reloadList, true); preferencesList.getAll(reloadList, true);

View file

@ -79,6 +79,11 @@ abstract public class ActivityWithNavigation extends EdgeToEdgeActivity {
} }
public void setOptionsCount(@NonNull Callable<Integer> getOptionsCount) {
this.getOptionsCount = getOptionsCount;
}
protected void resetKeyRepeat() { protected void resetKeyRepeat() {
lastKey = KeyEvent.KEYCODE_UNKNOWN; lastKey = KeyEvent.KEYCODE_UNKNOWN;
} }