Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Seems to work whether Flutter is the first acitivty or not
  • Loading branch information
justinmc committed Aug 18, 2023
commit 89629fb0643cc6db822f91d6058b984efc27b366
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnWindowFocusChangeListener;
import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -170,6 +172,8 @@ public class FlutterFragment extends Fragment
protected static final String ARG_SHOULD_AUTOMATICALLY_HANDLE_ON_BACK_PRESSED =
"should_automatically_handle_on_back_pressed";

private boolean hasRegisteredBackCallback = false;

@RequiresApi(18)
private final OnWindowFocusChangeListener onWindowFocusChangeListener =
Build.VERSION.SDK_INT >= 18
Expand Down Expand Up @@ -1023,6 +1027,20 @@ public void handleOnBackPressed() {
}
};

private final OnBackInvokedCallback onBackInvokedCallback =
Build.VERSION.SDK_INT >= 33
? new OnBackInvokedCallback() {
// TODO(garyq): Remove SuppressWarnings annotation. This was added to workaround
// a google3 bug where the linter is not properly running against API 33, causing
// a failure here. See b/243609613 and https://github.com/flutter/flutter/issues/111295
@SuppressWarnings("Override")
@Override
public void onBackInvoked() {
onBackPressed();
}
}
: null;

public FlutterFragment() {
// Ensure that we at least have an empty Bundle of arguments so that we don't
// need to continually check for null arguments before grabbing one.
Expand Down Expand Up @@ -1060,9 +1078,11 @@ public void onAttach(@NonNull Context context) {
super.onAttach(context);
delegate = delegateFactory.createDelegate(this);
delegate.onAttach(context);
/*
if (getArguments().getBoolean(ARG_SHOULD_AUTOMATICALLY_HANDLE_ON_BACK_PRESSED, false)) {
requireActivity().getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
}
*/
context.registerComponentCallbacks(this);
}

Expand Down Expand Up @@ -1684,6 +1704,55 @@ public boolean popSystemNavigator() {
return false;
}

@Override
public void setFrameworkHandlesBack(boolean frameworkHandlesBack) {
Log.e(
"justin",
"setFrameworkHandlesBack in FlutterFragment! setEnabled: " + frameworkHandlesBack);
if (frameworkHandlesBack && !hasRegisteredBackCallback) {
registerOnBackInvokedCallback();
} else if (!frameworkHandlesBack && hasRegisteredBackCallback) {
unregisterOnBackInvokedCallback();
}
}

/**
* Registers the callback with OnBackPressedDispatcher to capture back navigation gestures and
* pass them to the framework.
*
* <p>This replaces the deprecated onBackPressed method override in order to support API 33's
* predictive back navigation feature.
*
* <p>The callback must be unregistered in order to prevent unpredictable behavior once outside
* the Flutter app.
*/
@VisibleForTesting
public void registerOnBackInvokedCallback() {
if (Build.VERSION.SDK_INT >= 33) {
getActivity()
.getOnBackInvokedDispatcher()
.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback);
hasRegisteredBackCallback = true;
}
}

/**
* Unregisters the callback from OnBackPressedDispatcher.
*
* <p>This should be called when the activity is no longer in use to prevent unpredictable
* behavior such as being stuck and unable to press back.
*/
@VisibleForTesting
public void unregisterOnBackInvokedCallback() {
if (Build.VERSION.SDK_INT >= 33) {
getActivity()
.getOnBackInvokedDispatcher()
.unregisterOnBackInvokedCallback(onBackInvokedCallback);
hasRegisteredBackCallback = false;
}
}

@VisibleForTesting
@NonNull
boolean shouldDelayFirstAndroidViewDraw() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@ protected FlutterFragment createFlutterFragment() {
.shouldAttachEngineToActivity(shouldAttachEngineToActivity())
.destroyEngineWithFragment(shouldDestroyEngineWithHost())
.shouldDelayFirstAndroidViewDraw(shouldDelayFirstAndroidViewDraw)
// TODO(justinmc): Unsure whether this is relevant to predictive back.
// .shouldAutomaticallyHandleOnBackPressed(Build.VERSION.SDK_INT >= 33)
.build();
} else {
Log.v(
Expand Down