Skip to content

Commit fdeed0f

Browse files
committed
fix: restore in-app back navigation under predictive back
MainActivity overrode onBackPressed() to return to the home view from the settings and info screens. Apps targeting SDK 36 can no longer opt out of predictive back, and the platform now routes back gestures through OnBackPressedDispatcher instead of calling onBackPressed(), so back was leaving the app instead of returning home. Lint flags this as GestureBackNavigation, and only at targetSdk 36 -- the bump in #623 is what introduced the regression. Replace the override with an OnBackPressedCallback registered on the dispatcher. The callback is enabled only while a non-home view is showing, mirroring the existing up-arrow logic in chooseView(), so back still exits the app from the home view. isShowingHomeView() is now unused and removed. This needs androidx.activity 1.6+ to bridge the platform callback; the pinned appcompat 1.4.0 only brings in activity 1.2.4. Upgrade appcompat to 1.7.1, which pulls activity 1.8.0 and still supports minSdk 21. Deliberately not 1.8.0, which would require raising minSdk to 23.
1 parent 3d26924 commit fdeed0f

3 files changed

Lines changed: 300 additions & 15 deletions

File tree

‎Android/app/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ dependencies {
118118
androidTestImplementation 'androidx.annotation:annotation:1.3.0'
119119
androidTestImplementation 'androidx.test:runner:1.4.0'
120120
// UI libraries.
121-
implementation 'androidx.appcompat:appcompat:1.4.0'
121+
implementation 'androidx.appcompat:appcompat:1.7.1'
122122
implementation 'com.google.android.material:material:1.4.0'
123123
implementation 'androidx.preference:preference:1.1.1'
124124
implementation 'androidx.recyclerview:recyclerview:1.2.1'

‎Android/app/src/main/java/app/intra/ui/MainActivity.java‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import android.widget.Switch;
5252
import android.widget.TextView;
5353
import android.widget.Toast;
54+
import androidx.activity.OnBackPressedCallback;
5455
import androidx.annotation.DrawableRes;
5556
import androidx.annotation.IdRes;
5657
import androidx.annotation.StringRes;
@@ -177,6 +178,8 @@ protected void onCreate(Bundle savedInstanceState) {
177178

178179
setContentView(R.layout.activity_main);
179180

181+
getOnBackPressedDispatcher().addCallback(this, backToHomeCallback);
182+
180183
// Set up the toolbar
181184
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
182185
setSupportActionBar(toolbar);
@@ -722,20 +725,23 @@ private void syncDnsStatus() {
722725
}
723726
}
724727

725-
@Override
726-
public void onBackPressed() {
727-
if (isShowingHomeView()) {
728-
// Back button should leave the app if we are currently looking at the home view.
729-
super.onBackPressed();
730-
return;
728+
/**
729+
* Handles the back button on the settings and info views by returning to the home view.
730+
*
731+
* <p>This must not be an {@code onBackPressed()} override. Apps targeting SDK 36 can no
732+
* longer opt out of predictive back, and the platform routes back gestures through
733+
* {@link androidx.activity.OnBackPressedDispatcher} instead of calling
734+
* {@code onBackPressed()}.
735+
*
736+
* <p>The callback stays disabled while the home view is showing, so that back exits the
737+
* app as usual. {@link #chooseView} keeps that state in sync.
738+
*/
739+
private final OnBackPressedCallback backToHomeCallback = new OnBackPressedCallback(false) {
740+
@Override
741+
public void handleOnBackPressed() {
742+
chooseView(R.id.frame_main);
731743
}
732-
chooseView(R.id.frame_main);
733-
}
734-
735-
private boolean isShowingHomeView() {
736-
View home = findViewById(R.id.frame_main);
737-
return home.getVisibility() == View.VISIBLE;
738-
}
744+
};
739745

740746
private void showSettings() {
741747
settingsFragment = new SettingsFragment();
@@ -781,6 +787,10 @@ private View chooseView(int id) {
781787
drawerToggle.setDrawerIndicatorEnabled(false);
782788
actionBar.setDisplayHomeAsUpEnabled(true);
783789
}
790+
791+
// Intercept back only on the screens the arrow points away from. On the home view the
792+
// callback is disabled, so back falls through to the system and leaves the app.
793+
backToHomeCallback.setEnabled(id != R.id.frame_main);
784794
return selected;
785795
}
786796

0 commit comments

Comments
 (0)