|
21 | 21 | import android.os.Bundle; |
22 | 22 | import android.view.MenuItem; |
23 | 23 |
|
| 24 | +import androidx.activity.OnBackPressedCallback; |
24 | 25 | import androidx.annotation.NonNull; |
25 | 26 | import androidx.appcompat.app.AppCompatActivity; |
26 | 27 | import androidx.fragment.app.Fragment; |
@@ -80,6 +81,21 @@ protected void onCreate(Bundle savedInstanceState) { |
80 | 81 | "unexpected numActiveFrags = " + numActiveFrags); |
81 | 82 | } |
82 | 83 | }); |
| 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 | + }); |
83 | 99 | } |
84 | 100 |
|
85 | 101 | /** |
@@ -140,7 +156,7 @@ public boolean onOptionsItemSelected(MenuItem item) { |
140 | 156 | // To get a consistent behavior, both pressing "back" and clicking the Up arrow |
141 | 157 | // will navigate back, so if a preference category is shown, pressing the Up |
142 | 158 | // button won't close the settings, it will go back to the Index |
143 | | - super.onBackPressed(); |
| 159 | + getOnBackPressedDispatcher().onBackPressed(); |
144 | 160 | return true; |
145 | 161 | } |
146 | 162 | return super.onOptionsItemSelected(item); |
|
0 commit comments