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

How will Flutter implement Android 13's predictive back gesture? #109513

Open
victor-marino opened this issue Aug 14, 2022 · 13 comments
Open

How will Flutter implement Android 13's predictive back gesture? #109513

victor-marino opened this issue Aug 14, 2022 · 13 comments
Assignees
Labels
customer: crowd Affects or could affect many people, though not necessarily a specific customer. e: OS Version specific engine flutter/engine repository. See also e: labels. f: gestures flutter/packages/flutter/gestures repository. f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. new feature Nothing broken; request for a new capability. P3 Priority 3 issue (the default for issues we're likely to work on after P0-P2 issues) platform-android Android applications specifically. proposal A detailed proposal for a change to Flutter

Comments

@victor-marino
Copy link

victor-marino commented Aug 14, 2022

Use case

According to this page, we should get our apps ready for the new predictive back gesture that will be introduced in Android 13.

However, with just a few weeks to go before the final release, I haven't seen any info on how this will be handled in Flutter apps?

I have tried the simple trick of just adding android:enableOnBackInvokedCallback="true" to my Android Manifest, which does indeed activate the new animation on my Pixel 4 running the Android 13 beta. However, because all the back gestures are now captured by the system rather than Flutter, it completely breaks Flutter's navigation system, as ALL back gestures will now send you back to your phone's home screen, rather than taking you one level up Flutter's navigation tree when appropriate.

Proposal

I'm merely asking for information here, as I've searched extensively but couldn't find anything pertaining Flutter.

Will we have to do anything to adapt our apps? Or will the Flutter team implement this at some point at framework level? If it's the latter, when can we expect this to be added? Android 13 is right around the corner.

I'm assuming this should be handled at framework level, so that Flutter automatically implements this animation when navigating inside's Flutter's own tree, and hands it over to the system when the user is already at the root of the navigation stack (so they can correctly peek at their home screen before completing the gesture).

Eager to get some info on this!

@danagbemava-nc danagbemava-nc added in triage Presently being triaged by the triage team. new feature Nothing broken; request for a new capability. platform-android Android applications specifically. framework flutter/packages/flutter repository. See also f: labels. f: gestures flutter/packages/flutter/gestures repository. e: OS Version specific proposal A detailed proposal for a change to Flutter and removed in triage Presently being triaged by the triage team. labels Aug 15, 2022
@timsneath
Copy link
Member

Thanks so much for filing this issue!

Just to clarify, this is a feature being introduced as part of a multi-year transition to how Android handles back gestures. While it's available to developers for the first time in Android 13 through a Developer Options setting, it's not currently accessible to users and won't be until a future Android release. So while the clock is indeed 'ticking' and we will work on enabling support for this, this is not something that Android 13 app authors need to worry about immediately.

@victor-marino
Copy link
Author

Thanks a lot for your quick response! That clears things up for me.

I thought it would be available to regular users from the final release of Android 13 (without the need to flip the developer switch), but if that's not the case it's indeed not something to worry about for now.

@danagbemava-nc danagbemava-nc added the customer: crowd Affects or could affect many people, though not necessarily a specific customer. label Aug 15, 2022
@HansMuller HansMuller added the f: material design flutter/packages/flutter/material repository. label Aug 16, 2022
@goderbauer goderbauer added engine flutter/engine repository. See also e: labels. P2 Priority 2 issue likely blocking a tier-1 customer soon. P3 Priority 3 issue (the default for issues we're likely to work on after P0-P2 issues) and removed P2 Priority 2 issue likely blocking a tier-1 customer soon. labels Aug 18, 2022
@goderbauer
Copy link
Member

My current understanding is that for this to work correctly, the framework will have to tell the engine (or android) up front whether it is going to handle the back gesture internally or whether Android should handle it.

@GaryQian I think you've been looking closer at the Android APIs. Does that match your understanding?

@chunhtai Does the Navigator already tell the engine that it can handle back internally? I seem to remember that we had to add this to support back on the web?

@chunhtai
Copy link
Contributor

@goderbauer Currently flutter always tells Android it will handle the pop. If flutter then figure there is nothing else to pop, it then programmatically pop the android activity.

For web, the only thing I am aware is the fake browser entry to capture the backward button. I don't think it can be reuse here.

@goderbauer
Copy link
Member

Thanks, so we will likely have to build something new here to tell the engine whether the framework can handle back or not upfront, if my understanding of the new Android API is correct.

@chunhtai
Copy link
Contributor

cc @HansMuller @hangyujin

I am still not sure whether we need to implement the animation. From the screenshot, it looks like this may only apply when popping the entire app. Is there any animation change when popping the in-app page?

@HansMuller
Copy link
Contributor

There is, although support for that is farther out.

@GaryQian
Copy link
Contributor

From looking at the API, it seems that we should always be telling Android that we handle the back gesture. Regardless of whether we implement predictive back or just legacy back on the framework side, we will be doing custom handling of the back gesture via OnBackPressedCallback (which is called by the OnBackPressedDispatcher).

However, we should add logic to let Android know when we run out of pages to pop. At this point, we should unregister the callback, and Android will then handle the next back as exiting the app.

In any case, the opt-in is in build.gradle and needs to be preconfigured. It shouldn't be possible (or necessary) for the app to dynamically inform the build whether to enable or disable predictive backnav if it is not already set before build. We should be able to support this with a combo of Embedding API migration, framework predictive backnav implementation, and unregistering callbacks on final framework pop.

This is from an initial study of the API, I'll do some experimentation to see how it actually handles.

@victor-marino
Copy link
Author

victor-marino commented Aug 19, 2022

Hi all.

Just in case it's of any help, as I had already done some limited testing on my Pixel 4 running the Android 13 beta:

  1. Even if I flip the developer switch for the predictive back gesture on my phone, my app won't show the gesture at all unless I have explicitly opted-in in the build.gradle, not even for the final gesture to go back to the homescreen. So unless something is done on Flutter/app side, the gesture will definitely not appear at all.
  2. If I opt-in in the build.gradle for my app, I do get the gesture working to back out of my app and into the homescreen. Bad news is, now EVERY back gesture takes me out of my app, even if there are still pages left to pop inside my Flutter app. I assume now every back gesture is being handled by the OS, so it just breaks Flutter's internal navigation.

So I think everything GaryQian said matches with my experience.

@stuartmorgan
Copy link
Contributor

2. now EVERY back gesture takes me out of my app, even if there are still pages left to pop inside my Flutter app. I assume now every back gesture is being handled by the OS

This will be fixed by #109558. Enabling the opt-in turns off the older back handling system.

@fullflash
Copy link

still back button detaches android app willpopscope not event catches back button event

Android 13 Samsung device.
latest flutter sdk

@deimantasa
Copy link

still back button detaches android app willpopscope not event catches back button event

Android 13 Samsung device. latest flutter sdk

We had this issue as well. Few reasons it might happen:

  1. Check your AndroidManifest.xml and remove if you've android:enableOnBackInvokedCallback="true". We had it for some reason (I guess is a leftover from the past), which caused this issue
  2. Check your MainActivity (java or kotlin). It might extend FlutterFragmentActivity. If android:enableOnBackInvokedCallback is true in your manifest and FlutterFragmentActivity is extended, it seems to not work as well. However, what we've noticed that if android:enableOnBackInvokedCallback is set to true and MainActivity extends FlutterActivity - back button works as expected

Hope it helps.

dreautall added a commit to dreautall/waterfly-iii that referenced this issue Jun 2, 2023
predictive back is not yet supported by flutter, see
flutter/flutter#109513
@Akatsukishen
Copy link

@deimantasa You have done me a great favor. Thank you. When MainActivity extends FlutterFragmentActivity, the splash screen only show once, and it does not show when back to home and reopen app. Change MainActivity extends FlutterActivity, it shows every time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer: crowd Affects or could affect many people, though not necessarily a specific customer. e: OS Version specific engine flutter/engine repository. See also e: labels. f: gestures flutter/packages/flutter/gestures repository. f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. new feature Nothing broken; request for a new capability. P3 Priority 3 issue (the default for issues we're likely to work on after P0-P2 issues) platform-android Android applications specifically. proposal A detailed proposal for a change to Flutter
Projects
None yet
Development

No branches or pull requests