Skip to content

Commit 749fffc

Browse files
implement back button callback in PrefsActivity
1 parent 2923f05 commit 749fffc

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

app/src/main/java/com/nononsenseapps/notepad/prefs/PrefsActivity.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.os.Bundle;
2222
import android.view.MenuItem;
2323

24+
import androidx.activity.OnBackPressedCallback;
2425
import androidx.annotation.NonNull;
2526
import androidx.appcompat.app.AppCompatActivity;
2627
import androidx.fragment.app.Fragment;
@@ -80,6 +81,21 @@ protected void onCreate(Bundle savedInstanceState) {
8081
"unexpected numActiveFrags = " + numActiveFrags);
8182
}
8283
});
84+
85+
// when pressing the physical back button, navigate between fragments by removing the
86+
// subtitle
87+
getOnBackPressedDispatcher().addCallback(this,
88+
new OnBackPressedCallback(true) {
89+
@Override
90+
public void handleOnBackPressed() {
91+
// replicate super.onBackPressed() behavior
92+
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
93+
getSupportFragmentManager().popBackStack();
94+
} else {
95+
finish();
96+
}
97+
}
98+
});
8399
}
84100

85101
/**
@@ -140,7 +156,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
140156
// To get a consistent behavior, both pressing "back" and clicking the Up arrow
141157
// will navigate back, so if a preference category is shown, pressing the Up
142158
// button won't close the settings, it will go back to the Index
143-
super.onBackPressed();
159+
getOnBackPressedDispatcher().onBackPressed();
144160
return true;
145161
}
146162
return super.onOptionsItemSelected(item);

0 commit comments

Comments
 (0)