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

FlutterFragment predictive back #44865

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,11 @@ public void onAttach(@NonNull Context context) {
delegate.onAttach(context);
if (getArguments().getBoolean(ARG_SHOULD_AUTOMATICALLY_HANDLE_ON_BACK_PRESSED, false)) {
requireActivity().getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
// By default, Android handles backs, and predictive back is enabled. This
// can be changed by calling setFrameworkHandlesBack. For example, the
// framework will call this automatically in a typical app when it has
// routes to pop.
onBackPressedCallback.setEnabled(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a potentially dangerous change. Does the framework guarantee that the callback will always be enabled when applicable via setFrameworkHandlesBack, regardless of API version?

}
context.registerComponentCallbacks(this);
}
Expand Down Expand Up @@ -1684,6 +1689,14 @@ public boolean popSystemNavigator() {
return false;
}

@Override
public void setFrameworkHandlesBack(boolean frameworkHandlesBack) {
if (getArguments().getBoolean(ARG_SHOULD_AUTOMATICALLY_HANDLE_ON_BACK_PRESSED, false)) {
return;
}
onBackPressedCallback.setEnabled(frameworkHandlesBack);
}

@VisibleForTesting
@NonNull
boolean shouldDelayFirstAndroidViewDraw() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ protected FlutterFragment createFlutterFragment() {
? TransparencyMode.opaque
: TransparencyMode.transparent;
final boolean shouldDelayFirstAndroidViewDraw = renderMode == RenderMode.surface;
final boolean shouldAutomaticallyHandleOnBackPressed = Build.VERSION.SDK_INT >= 33;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, you'll need to update the implementation of onBackPressed() below. On Android 33, the onBackPressed() method is still relevant, but it won't behave correctly without a call to super.onBackPressed().

Alternatively, you could make this condition always true and fully remove the onBackPressed() override below. That way you have less conditional behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose removing the onBackPressed() override could impact behavior for any clients that subclass FlutterFragmentActivity and themselves override the method, but given that Android now formally discourages that anyways, that seems like a reasonable breaking change.


if (getCachedEngineId() != null) {
Log.v(
Expand All @@ -545,6 +546,7 @@ protected FlutterFragment createFlutterFragment() {
.shouldAttachEngineToActivity(shouldAttachEngineToActivity())
.destroyEngineWithFragment(shouldDestroyEngineWithHost())
.shouldDelayFirstAndroidViewDraw(shouldDelayFirstAndroidViewDraw)
.shouldAutomaticallyHandleOnBackPressed(shouldAutomaticallyHandleOnBackPressed)
.build();
} else {
Log.v(
Expand Down Expand Up @@ -581,6 +583,7 @@ protected FlutterFragment createFlutterFragment() {
.transparencyMode(transparencyMode)
.shouldAttachEngineToActivity(shouldAttachEngineToActivity())
.shouldDelayFirstAndroidViewDraw(shouldDelayFirstAndroidViewDraw)
.shouldAutomaticallyHandleOnBackPressed(shouldAutomaticallyHandleOnBackPressed)
.build();
}

Expand All @@ -596,6 +599,7 @@ protected FlutterFragment createFlutterFragment() {
.transparencyMode(transparencyMode)
.shouldAttachEngineToActivity(shouldAttachEngineToActivity())
.shouldDelayFirstAndroidViewDraw(shouldDelayFirstAndroidViewDraw)
.shouldAutomaticallyHandleOnBackPressed(shouldAutomaticallyHandleOnBackPressed)
.build();
}
}
Expand Down