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
Show all changes
22 commits
Select commit Hold shift + click to select a range
9b25e71
Log everywhere I see onBackPressed... And it doesn't log anything!
justinmc Jan 27, 2023
b10efe3
Just hackily remove all usage of onBack, and predictive back does wor…
justinmc Jan 27, 2023
75d8e38
Undid all of the commenting and deleting and located the one call tha…
justinmc Jan 27, 2023
e8a5130
No logging verbose
justinmc Jan 27, 2023
9c0e7cc
WIP A platform channel API to declare that the nav stack is empty
justinmc Jan 28, 2023
205d1e5
navigationStackIsEmpty method to enable predictive back
justinmc Feb 1, 2023
66d1f84
Migrate to updateNavigationStackStatus, works for inner routes
justinmc Feb 9, 2023
9e8e230
Clean up logs and todos
justinmc Feb 9, 2023
0ed2bec
Rename hasMultiple to frameworkHandlesPop for clarity
justinmc Mar 7, 2023
2afbbad
Merge branch 'main' into predictive-back-root
justinmc Mar 7, 2023
8aeba57
Analyzer fix
justinmc Mar 7, 2023
5e89211
WIP some debugging
justinmc Apr 7, 2023
015f2a5
Merge branch 'main' into predictive-back-root
justinmc Apr 7, 2023
b7b2f17
Merge branch 'main' into predictive-back-root
justinmc May 12, 2023
6bc7d5e
Rename to frameworkHandlesBacks
justinmc May 26, 2023
01a22a2
Clean up
justinmc May 26, 2023
6f22009
Confirmed that it works on my old (API 24) samsung device
justinmc May 26, 2023
440a6ee
I dont think I need to modify flutterfragment(activity)
justinmc May 30, 2023
e075275
Tests and empty implementation for FlutterFragment
justinmc Jun 2, 2023
af98e2c
Merge remote-tracking branch 'upstream/main' into predictive-back-root
justinmc Jun 5, 2023
1db22e5
More specific name for hasRegisteredCallback
justinmc Jun 7, 2023
4701d66
Backs to Back
justinmc Jun 7, 2023
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
Prev Previous commit
Next Next commit
Just hackily remove all usage of onBack, and predictive back does wor…
…k now
  • Loading branch information
justinmc committed Jan 27, 2023
commit b10efe389707d9560da99c0a1b6963f6d83a0817
2 changes: 2 additions & 0 deletions shell/platform/android/io/flutter/app/FlutterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ protected void onDestroy() {
super.onDestroy();
}

/*
@Override
public void onBackPressed() {
Log.v("justin", "onBackPressed in FlutterActivity");
if (!eventDelegate.onBackPressed()) {
super.onBackPressed();
}
}
*/

@Override
protected void onStop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public void onDestroy() {
}
}

/*
@Override
public boolean onBackPressed() {
Log.v("justin", "onBackPressed in FlutterActivityDelegate");
Expand All @@ -255,6 +256,7 @@ public boolean onBackPressed() {
}
return false;
}
*/

@Override
public void onUserLeaveHint() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public interface FlutterActivityEvents
* continue with its default back button handling.
* @see android.app.Activity#onBackPressed()
*/
/*
boolean onBackPressed();
*/

/** @see android.app.Activity#onUserLeaveHint() */
void onUserLeaveHint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ protected void onDestroy() {
super.onDestroy();
}

/*
@Override
public void onBackPressed() {
Log.v("justin", "onBackPressed ok in FlutterFragmentActivity");
if (!eventDelegate.onBackPressed()) {
super.onBackPressed();
}
}
*/

@Override
protected void onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;
//import android.window.OnBackInvokedCallback;
//import android.window.OnBackInvokedDispatcher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
Expand Down Expand Up @@ -643,7 +643,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);

registerOnBackInvokedCallback();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that this is removed, it means that by default, Flutter will not handle back gestures. So if you start up a Flutter app that does nothing, has no Navigator or WidgetsApp etc., root predictive back will work. The framework will have to first call setFrameworkHandlesBacks in order to prevent predictive back and to handle back gestures itself. This will be handled automatically in Flutter apps that use WidgetsApp.

//registerOnBackInvokedCallback();

configureWindowForTransparency();

Expand All @@ -662,6 +662,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
* <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) {
Expand All @@ -670,20 +671,24 @@ public void registerOnBackInvokedCallback() {
OnBackInvokedDispatcher.PRIORITY_DEFAULT, onBackInvokedCallback);
}
}
*/

/**
* Unregisters the callback from OnBackInvokedDispatcher.
*
* <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) {
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(onBackInvokedCallback);
}
}
*/

/*
private final OnBackInvokedCallback onBackInvokedCallback =
Build.VERSION.SDK_INT >= 33
? new OnBackInvokedCallback() {
Expand All @@ -698,6 +703,7 @@ public void onBackInvoked() {
}
}
: null;
*/

/**
* Switches themes for this {@code Activity} from the theme used to launch this {@code Activity}
Expand Down Expand Up @@ -879,7 +885,7 @@ protected void onSaveInstanceState(Bundle outState) {
*/
@VisibleForTesting
public void release() {
unregisterOnBackInvokedCallback();
//unregisterOnBackInvokedCallback();
if (delegate != null) {
delegate.release();
delegate = null;
Expand Down Expand Up @@ -928,12 +934,15 @@ protected void onNewIntent(@NonNull Intent intent) {
}
}

/*
@Override
public void onBackPressed() {
Log.v("justin", "onBackPressed in FlutterActivity (embedder)");
if (stillAttachedForEvent("onBackPressed")) {
delegate.onBackPressed();
}
}
*/

@Override
public void onRequestPermissionsResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ void onDetach() {
*
* <p>This method instructs Flutter's navigation system to "pop route".
*/
/*
void onBackPressed() {
Log.v("justin", "onBackPressed in FlutterFragmentAndFragmentDelegate");
ensureAlive();
Expand All @@ -787,6 +788,7 @@ void onBackPressed() {
Log.w(TAG, "Invoked onBackPressed() before FlutterFragment was attached to an Activity.");
}
}
*/

/**
* Invoke this from {@link android.app.Activity#onRequestPermissionsResult(int, String[], int[])}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.activity.OnBackPressedCallback;
//import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
Expand All @@ -36,14 +36,11 @@
*
* <ol>
* <li>{@link #onPostResume()}
* <li>{@link #onBackPressed()}
* <li>{@link #onRequestPermissionsResult(int, String[], int[])}
* <li>{@link #onNewIntent(Intent)}
* <li>{@link #onUserLeaveHint()}
* </ol>
*
* {@link #onBackPressed()} does not need to be called through if the fragment is constructed by one
* of the builders with {@code shouldAutomaticallyHandleOnBackPressed(true)}.
*
* <p>Additionally, when starting an {@code Activity} for a result from this {@code Fragment}, be
* sure to invoke {@link Fragment#startActivityForResult(Intent, int)} rather than {@link
Expand Down Expand Up @@ -161,7 +158,6 @@ public class FlutterFragment extends Fragment
*/
protected static final String ARG_ENABLE_STATE_RESTORATION = "enable_state_restoration";
/**
* True if the fragment should receive {@link #onBackPressed()} events automatically, without
* requiring an explicit activity call through.
*/
protected static final String ARG_SHOULD_AUTOMATICALLY_HANDLE_ON_BACK_PRESSED =
Expand Down Expand Up @@ -391,13 +387,10 @@ public NewEngineFragmentBuilder shouldAttachEngineToActivity(
}

/**
* Whether or not this {@code FlutterFragment} should automatically receive {@link
* #onBackPressed()} events, rather than requiring an explicit activity call through. Disabled
* Whether or not this {@code FlutterFragment} should automatically receive @link
* by default.
*
* <p>When enabled, the activity will automatically dispatch back-press events to the fragment's
* {@link OnBackPressedCallback}, instead of requiring the activity to manually call {@link
* #onBackPressed()} in client code. If enabled, do <b>not</b> invoke {@link #onBackPressed()}
* manually.
*
* <p>This behavior relies on the implementation of {@link #popSystemNavigator()}. It's not
Expand All @@ -408,7 +401,8 @@ public NewEngineFragmentBuilder shouldAttachEngineToActivity(
@NonNull
public NewEngineFragmentBuilder shouldAutomaticallyHandleOnBackPressed(
boolean shouldAutomaticallyHandleOnBackPressed) {
this.shouldAutomaticallyHandleOnBackPressed = shouldAutomaticallyHandleOnBackPressed;
//this.shouldAutomaticallyHandleOnBackPressed = shouldAutomaticallyHandleOnBackPressed;
this.shouldAutomaticallyHandleOnBackPressed = false;//shouldAutomaticallyHandleOnBackPressed;
return this;
}

Expand Down Expand Up @@ -648,13 +642,9 @@ public CachedEngineFragmentBuilder shouldAttachEngineToActivity(
}

/**
* Whether or not this {@code FlutterFragment} should automatically receive {@link
* #onBackPressed()} events, rather than requiring an explicit activity call through. Disabled
* by default.
*
* <p>When enabled, the activity will automatically dispatch back-press events to the fragment's
* {@link OnBackPressedCallback}, instead of requiring the activity to manually call {@link
* #onBackPressed()} in client code. If enabled, do <b>not</b> invoke {@link #onBackPressed()}
* manually.
*
* <p>Enabling this behavior relies on explicit behavior in {@link #popSystemNavigator()}. It's
Expand All @@ -665,7 +655,8 @@ public CachedEngineFragmentBuilder shouldAttachEngineToActivity(
@NonNull
public CachedEngineFragmentBuilder shouldAutomaticallyHandleOnBackPressed(
boolean shouldAutomaticallyHandleOnBackPressed) {
this.shouldAutomaticallyHandleOnBackPressed = shouldAutomaticallyHandleOnBackPressed;
//this.shouldAutomaticallyHandleOnBackPressed = shouldAutomaticallyHandleOnBackPressed;
this.shouldAutomaticallyHandleOnBackPressed = false;//shouldAutomaticallyHandleOnBackPressed;
return this;
}

Expand Down Expand Up @@ -899,14 +890,7 @@ public NewEngineInGroupFragmentBuilder shouldAttachEngineToActivity(
}

/**
* Whether or not this {@code FlutterFragment} should automatically receive {@link
* #onBackPressed()} events, rather than requiring an explicit activity call through. Disabled
* by default.
*
* <p>When enabled, the activity will automatically dispatch back-press events to the fragment's
* {@link OnBackPressedCallback}, instead of requiring the activity to manually call {@link
* #onBackPressed()} in client code. If enabled, do <b>not</b> invoke {@link #onBackPressed()}
* manually.
* Whether or not this {@code FlutterFragment} should automatically receive
*
* <p>This behavior relies on the implementation of {@link #popSystemNavigator()}. It's not
* recommended to override that method when enabling this attribute, but if you do, you should
Expand All @@ -916,7 +900,8 @@ public NewEngineInGroupFragmentBuilder shouldAttachEngineToActivity(
@NonNull
public NewEngineInGroupFragmentBuilder shouldAutomaticallyHandleOnBackPressed(
boolean shouldAutomaticallyHandleOnBackPressed) {
this.shouldAutomaticallyHandleOnBackPressed = shouldAutomaticallyHandleOnBackPressed;
//this.shouldAutomaticallyHandleOnBackPressed = shouldAutomaticallyHandleOnBackPressed;
this.shouldAutomaticallyHandleOnBackPressed = false;
return this;
}

Expand Down Expand Up @@ -999,6 +984,7 @@ public FlutterActivityAndFragmentDelegate createDelegate(
return new FlutterActivityAndFragmentDelegate(host);
}

/*
private final OnBackPressedCallback onBackPressedCallback =
new OnBackPressedCallback(true) {
@Override
Expand All @@ -1007,6 +993,7 @@ public void handleOnBackPressed() {
onBackPressed();
}
};
*/

public FlutterFragment() {
// Ensure that we at least have an empty Bundle of arguments so that we don't
Expand Down Expand Up @@ -1046,7 +1033,10 @@ public void onAttach(@NonNull Context context) {
delegate = delegateFactory.createDelegate(this);
delegate.onAttach(context);
if (getArguments().getBoolean(ARG_SHOULD_AUTOMATICALLY_HANDLE_ON_BACK_PRESSED, false)) {
/*
Log.v("justin", "onBackPressedDispatcher addCallback in FlutterFragment");
requireActivity().getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
*/
}
context.registerComponentCallbacks(this);
}
Expand Down Expand Up @@ -1197,15 +1187,16 @@ public void onNewIntent(@NonNull Intent intent) {
* <p>If the fragment uses {@code shouldAutomaticallyHandleOnBackPressed(true)}, this method
* should not be called through. It will be called automatically instead.
*
* <p>See {@link android.app.Activity#onBackPressed()}
*/
/*
@ActivityCallThrough
public void onBackPressed() {
Log.v("justin", "onBackPressed in FlutterFragment (embedder)");
if (stillAttachedForEvent("onBackPressed")) {
delegate.onBackPressed();
}
}
*/

/**
* A result has been returned after an invocation of {@link
Expand Down Expand Up @@ -1635,7 +1626,7 @@ public boolean shouldDispatchAppLifecycleState() {
* <p>Avoid overriding this method when using {@code
* shouldAutomaticallyHandleOnBackPressed(true)}. If you do, you must always {@code return
* super.popSystemNavigator()} rather than {@code return false}. Otherwise the navigation behavior
* will recurse infinitely between this method and {@link #onBackPressed()}, breaking navigation.
* will recurse infinitely between this method and @link #onBackPressed()}, breaking navigation.
*/
@Override
public boolean popSystemNavigator() {
Expand All @@ -1645,10 +1636,12 @@ public boolean popSystemNavigator() {
// Unless we disable the callback, the dispatcher call will trigger it. This will then
// trigger the fragment's onBackPressed() implementation, which will call through to the
// dart side and likely call back through to this method, creating an infinite call loop.
/*
Log.v("justin", "changine enabled on onBackPressedCallback");
onBackPressedCallback.setEnabled(false);
activity.getOnBackPressedDispatcher().onBackPressed();
onBackPressedCallback.setEnabled(true);
*/
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,13 @@ protected void onNewIntent(@NonNull Intent intent) {
super.onNewIntent(intent);
}

/*
@Override
public void onBackPressed() {
Log.v("justin", "onBackPressed in FlutterFragmentActivity (embedder)");
flutterFragment.onBackPressed();
}
*/

@Override
public void onRequestPermissionsResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.activity.OnBackPressedDispatcherOwner;
//import androidx.activity.OnBackPressedDispatcherOwner;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
Expand Down Expand Up @@ -481,12 +481,15 @@ private void popSystemNavigator() {
return;
}

/*
if (activity instanceof OnBackPressedDispatcherOwner) {
Log.v("justin", "calling onBackPressed in popSystemNavigator in PlatformPlugin");
((OnBackPressedDispatcherOwner) activity).getOnBackPressedDispatcher().onBackPressed();
} else {
activity.finish();
}
*/
activity.finish();
}

private CharSequence getClipboardData(PlatformChannel.ClipboardContentFormat format) {
Expand Down