Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predictive back gesture Android #132504

Closed
2 tasks done
mariamhas opened this issue Aug 14, 2023 · 11 comments
Closed
2 tasks done

Predictive back gesture Android #132504

mariamhas opened this issue Aug 14, 2023 · 11 comments
Assignees
Labels
f: gestures flutter/packages/flutter/gestures repository. platform-android Android applications specifically r: fixed Issue is closed as already fixed in a newer version team-design Owned by Design Languages team

Comments

@mariamhas
Copy link

mariamhas commented Aug 14, 2023

umbrella issue for the adding support for the Android 14 feature, Predictive back navigation.

@danagbemava-nc danagbemava-nc added in triage Presently being triaged by the triage team platform-android Android applications specifically f: gestures flutter/packages/flutter/gestures repository. team-design Owned by Design Languages team and removed in triage Presently being triaged by the triage team labels Aug 15, 2023
@lukehutch
Copy link
Contributor

lukehutch commented Oct 30, 2023

Just wanted to point out that a lot of people are going to run into this issue, since adb is already warning:

W/OnBackInvokedCallback(13376): OnBackInvokedCallback is not enabled for the application.
W/OnBackInvokedCallback(13376): Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.

However, if you set this value, Flutter is never even passed traditional back button push events.

I set this value, following the directions in the logs, and then promptly forgot about it. A couple of weeks later realized that the back button had stopped working in my app. It took me a whole week to track down why Flutter wasn't even receiving back button events.

@justinmc
Copy link
Contributor

justinmc commented Nov 3, 2023

@lukehutch Thanks for pointing this out for anyone else with this problem. Can I ask how exactly you reproduce it? What version of Flutter were you using? If I just create a new Flutter app, set enableOnBackInvokedCallback to true, and run it on an Android emulator (API 34), the back button works.

@lukehutch
Copy link
Contributor

lukehutch commented Nov 4, 2023

@justinmc I'm on Flutter 3.13.9 (Android emulator, API 33). Tested again just now.

If I add android:enableOnBackInvokedCallback="true" to the application tag of app/main/AndroidManifest.xml, then the Flutter runtime is never sent back button events.

I debugged this all the way down to the method channel in Flutter, and the Back events are simply never received by Flutter.

One twist though is that if you push the Back button with this attribute added, then the app is popped when the Back button is pressed (i.e. Back returns to the home screen). In other words, it's not like the Back button does nothing at all, but it consistently closes the app. So the Android app generated by Flutter is probably still receiving the Back button, but it just doesn't do anything with it.

What this means though is that proper Back button behavior can't be implemented in Flutter, e.g. by means of a BackButtonDispatcher like the following, since it is never called:

/// Make back button close any open dialogs first, and only pop the route
/// if there are no dialogs open. (Default behavior is to pop the app on
/// every back button press)
class FixedBackButtonDispatcher extends RootBackButtonDispatcher {
  @override
  Future<bool> didPopRoute() async {
    // Close any open modal dialog before attempting to pop route
    if (navigatorKey.currentState?.canPop() ?? false) {
      navigatorKey.currentState!.pop();
      return true;
    }
    // Pop route, or pop app if at top level
    return super.didPopRoute();
  }
}

Incidentally, this is a separate issue, but it would be nice if the above BackButtonDispatcher behavior were made the default, when the new predictive back gesture support is put in place.

@shareefhadid
Copy link

Noticed this issue as well. What is the proper way for implementing android:enableOnBackInvokedCallback="true" in the AndroidManifest?

As mentioned, it currently breaks the back button on android (minimizes the app instead of popping the route in my case).

@justinmc
Copy link
Contributor

The main predictive back PR is #132249, and it is not yet on stable (merge commit f68d03f). I think the behavior you guys observed is expected when setting android:enableOnBackInvokedCallback="true" before that PR was merged. Can you try it again on beta or master?

@lukehutch
Copy link
Contributor

@justinmc I can confirm that the issue is fixed on the current Flutter beta channel. Thanks!

@dawarepramod4
Copy link

when will this come to Stable ..?

@justinmc
Copy link
Contributor

justinmc commented Jan 8, 2024

Status update

There are two main parts to this issue, 1. when navigating out of a Flutter app and 2. when navigating within a Flutter app.

1. Navigating out of the app

Completed in #132249 and currently on stable (see f68d03f).

2. Navigating within the app

In progress. Tracked in #131961.

Engine PR: flutter/engine#49093

Framework PR: #141373

@justinmc
Copy link
Contributor

All of the minimum PRs for this have been merged, so I'm closing this as completed. There will be follow-up work on OpenContainer transitions (flutter/packages#6321) and making it easier to write custom predictive back transitions, but I'll track those in their own PRs.

@justinmc
Copy link
Contributor

Let me give a full update on the state of predictive back right now.

Inter-app and intra-app predictive back are now supported by Flutter

Inter-app Intra-app
Navigating from your Flutter app to another app or to the homescreen with a back gesture. Navigating within your Flutter app's routes with a back gesture.
#132249 #141373

Setting up your app to support predictive back

Currently (Android API 34) predictive back is not even supported natively without jumping through some hoops. See the guide for how to get your Flutter app and device set up, including the part about migrating to PopScope if needed.

In addition, be sure you're on 3.21.0-16.0.pre or above, and you'll need to opt-in to using the new route transitions for now like this:

return MaterialApp(
  theme: ThemeData(
    brightness: Brightness.light,
    pageTransitionsTheme: const PageTransitionsTheme(
      builders: {
        // Use PredictiveBackPageTransitionsBuilder to get the predictive back route transition!
        TargetPlatform.android: PredictiveBackPageTransitionsBuilder(),
      },
    ),
  ),
  home: const MyApp(),
);

Though there's a lot of setup to do now, the idea is that this will all be the default in the long run. In the future native Android will support predictive back out of the box, and Flutter Android apps will default to the PredictiveBackPageTransitionsBuilder transitions.

Can I write my own predictive back route transition?

I wouldn't recommend it, yet. I plan to make some APIs public that will make this easier in the future. If you really want to mess with this now, look at how PredictiveBackPageTransitionsBuilder is implemented.

OpenContainer transitions with predictive back

Currently OpenContainer transitions don't support predictive back, but I'm working on (flutter/packages#6321) supporting them out of the box soon. I'm talking about the kind of transitions where the popped route transitions back into some element on the previous page, which is currently done with the animations package.

@danagbemava-nc danagbemava-nc added the r: fixed Issue is closed as already fixed in a newer version label Apr 1, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
f: gestures flutter/packages/flutter/gestures repository. platform-android Android applications specifically r: fixed Issue is closed as already fixed in a newer version team-design Owned by Design Languages team
Projects
None yet
Development

No branches or pull requests

6 participants