got rid of the the deprecated PreferencesActivity.onBackPressed()
This commit is contained in:
parent
c28c9f053e
commit
54d70c8cc2
4 changed files with 34 additions and 12 deletions
|
|
@ -15,6 +15,7 @@
|
|||
</queries>
|
||||
|
||||
<application
|
||||
android:enableOnBackInvokedCallback="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:label="@string/app_name"
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package io.github.sspanak.tt9.preferences;
|
|||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.activity.OnBackPressedCallback;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
|
|
@ -52,6 +53,7 @@ public class PreferencesActivity extends ActivityWithNavigation implements Prefe
|
|||
validateFunctionKeys();
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
setOnBackPressed();
|
||||
|
||||
// changing the theme causes onCreate(), which displays the MainSettingsScreen,
|
||||
// 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() {
|
||||
Fragment previousFragment = getSupportFragmentManager().findFragmentById(R.id.preferences_container);
|
||||
if (previousFragment instanceof BaseScreenFragment) {
|
||||
((BaseScreenFragment) previousFragment).onBackPressed();
|
||||
}
|
||||
/**
|
||||
* onBackPressed() is deprecated, so calling the onBackPressed() on the Fragments is now more complicated.
|
||||
*/
|
||||
private void setOnBackPressed() {
|
||||
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);
|
||||
if (nextFragment instanceof BaseScreenFragment) {
|
||||
((BaseScreenFragment) nextFragment).onBackPressed();
|
||||
getOptionsCount = ((BaseScreenFragment) nextFragment)::getPreferenceCount;
|
||||
}
|
||||
// On API >= 33, this requires android:enableOnBackInvokedCallback="true" in the manifest
|
||||
getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -66,6 +66,7 @@ abstract public class BaseScreenFragment extends PreferenceFragmentCompat {
|
|||
public void onResume() {
|
||||
super.onResume();
|
||||
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) {
|
||||
initPreferencesList();
|
||||
preferencesList.getAll(reloadList, true);
|
||||
|
|
|
|||
|
|
@ -79,6 +79,11 @@ abstract public class ActivityWithNavigation extends EdgeToEdgeActivity {
|
|||
}
|
||||
|
||||
|
||||
public void setOptionsCount(@NonNull Callable<Integer> getOptionsCount) {
|
||||
this.getOptionsCount = getOptionsCount;
|
||||
}
|
||||
|
||||
|
||||
protected void resetKeyRepeat() {
|
||||
lastKey = KeyEvent.KEYCODE_UNKNOWN;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue