Skip to content

Commit 9183bff

Browse files
authored
[flutter roll] Revert "Determine lifecycle by looking at window focus also" (flutter#41626)
Reverts flutter#41094. context: updated the java/android timeouts and details in b/280204906
1 parent 666bc34 commit 9183bff

16 files changed

+31
-469
lines changed

lib/ui/platform_dispatcher.dart

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,61 +1642,31 @@ class FrameTiming {
16421642
/// States that an application can be in.
16431643
///
16441644
/// The values below describe notifications from the operating system.
1645-
/// Applications should not expect to always receive all possible notifications.
1646-
/// For example, if the users pulls out the battery from the device, no
1647-
/// notification will be sent before the application is suddenly terminated,
1648-
/// along with the rest of the operating system.
1649-
///
1650-
/// For historical and name collision reasons, Flutter's application state names
1651-
/// do not correspond one to one with the state names on all platforms. On
1652-
/// Android, for instance, when the OS calls
1653-
/// [`Activity.onPause`](https://developer.android.com/reference/android/app/Activity#onPause()),
1654-
/// Flutter will enter the [inactive] state, but when Android calls
1655-
/// [`Activity.onStop`](https://developer.android.com/reference/android/app/Activity#onStop()),
1656-
/// Flutter enters the [paused] state. See the individual state's documentation
1657-
/// for descriptions of what they mean on each platform.
1645+
/// Applications should not expect to always receive all possible
1646+
/// notifications. For example, if the users pulls out the battery from the
1647+
/// device, no notification will be sent before the application is suddenly
1648+
/// terminated, along with the rest of the operating system.
16581649
///
16591650
/// See also:
16601651
///
1661-
/// * [WidgetsBindingObserver], for a mechanism to observe the lifecycle state
1662-
/// from the widgets layer.
1663-
/// * iOS's [IOKit activity lifecycle](https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle?language=objc) documentation.
1664-
/// * Android's [activity lifecycle](https://developer.android.com/guide/components/activities/activity-lifecycle) documentation.
1665-
/// * macOS's [AppKit activity lifecycle](https://developer.apple.com/documentation/appkit/nsapplicationdelegate?language=objc) documentation.
1652+
/// * [WidgetsBindingObserver], for a mechanism to observe the lifecycle state
1653+
/// from the widgets layer.
16661654
enum AppLifecycleState {
1667-
/// The application is visible and responsive to user input.
1668-
///
1669-
/// On Android, this state corresponds to the Flutter host view having focus
1670-
/// ([`Activity.onWindowFocusChanged`](https://developer.android.com/reference/android/app/Activity#onWindowFocusChanged(boolean))
1671-
/// was called with true) while in Android's "resumed" state. It is possible
1672-
/// for the Flutter app to be in the [inactive] state while still being in
1673-
/// Android's
1674-
/// ["onResume"](https://developer.android.com/guide/components/activities/activity-lifecycle)
1675-
/// state if the app has lost focus
1676-
/// ([`Activity.onWindowFocusChanged`](https://developer.android.com/reference/android/app/Activity#onWindowFocusChanged(boolean))
1677-
/// was called with false), but hasn't had
1678-
/// [`Activity.onPause`](https://developer.android.com/reference/android/app/Activity#onPause())
1679-
/// called on it.
1655+
/// The application is visible and responding to user input.
16801656
resumed,
16811657

16821658
/// The application is in an inactive state and is not receiving user input.
16831659
///
16841660
/// On iOS, this state corresponds to an app or the Flutter host view running
1685-
/// in the foreground inactive state. Apps transition to this state when in a
1686-
/// phone call, responding to a TouchID request, when entering the app
1661+
/// in the foreground inactive state. Apps transition to this state when in
1662+
/// a phone call, responding to a TouchID request, when entering the app
16871663
/// switcher or the control center, or when the UIViewController hosting the
16881664
/// Flutter app is transitioning.
16891665
///
1690-
/// On Android, this corresponds to an app or the Flutter host view running in
1691-
/// Android's paused state (i.e.
1692-
/// [`Activity.onPause`](https://developer.android.com/reference/android/app/Activity#onPause())
1693-
/// has been called), or in Android's "resumed" state (i.e.
1694-
/// [`Activity.onResume`](https://developer.android.com/reference/android/app/Activity#onResume())
1695-
/// has been called) but it has lost window focus. Examples of when apps
1696-
/// transition to this state include when the app is partially obscured or
1697-
/// another activity is focused, such as: a split-screen app, a phone call, a
1698-
/// picture-in-picture app, a system dialog, another view, when the
1699-
/// notification window shade is down, or the application switcher is visible.
1666+
/// On Android, this corresponds to an app or the Flutter host view running
1667+
/// in the foreground inactive state. Apps transition to this state when
1668+
/// another activity is focused, such as a split-screen app, a phone call,
1669+
/// a picture-in-picture app, a system dialog, or another view.
17001670
///
17011671
/// Apps in this state should assume that they may be [paused] at any time.
17021672
inactive,

shell/platform/android/io/flutter/app/FlutterActivity.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,6 @@ public void onUserLeaveHint() {
157157
eventDelegate.onUserLeaveHint();
158158
}
159159

160-
@Override
161-
public void onWindowFocusChanged(boolean hasFocus) {
162-
super.onWindowFocusChanged(hasFocus);
163-
eventDelegate.onWindowFocusChanged(hasFocus);
164-
}
165-
166160
@Override
167161
public void onTrimMemory(int level) {
168162
eventDelegate.onTrimMemory(level);

shell/platform/android/io/flutter/app/FlutterActivityDelegate.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,6 @@ public void onUserLeaveHint() {
260260
flutterView.getPluginRegistry().onUserLeaveHint();
261261
}
262262

263-
@Override
264-
public void onWindowFocusChanged(boolean hasFocus) {
265-
flutterView.getPluginRegistry().onWindowFocusChanged(hasFocus);
266-
}
267-
268263
@Override
269264
public void onTrimMemory(int level) {
270265
// Use a trim level delivered while the application is running so the

shell/platform/android/io/flutter/app/FlutterActivityEvents.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,4 @@ public interface FlutterActivityEvents
6464

6565
/** @see android.app.Activity#onUserLeaveHint() */
6666
void onUserLeaveHint();
67-
68-
/**
69-
* @param hasFocus True if the current activity window has focus.
70-
* @see android.app.Activity#onWindowFocusChanged(boolean)
71-
*/
72-
void onWindowFocusChanged(boolean hasFocus);
7367
}

shell/platform/android/io/flutter/app/FlutterFragmentActivity.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,6 @@ public void onUserLeaveHint() {
155155
eventDelegate.onUserLeaveHint();
156156
}
157157

158-
@Override
159-
public void onWindowFocusChanged(boolean hasFocus) {
160-
super.onWindowFocusChanged(hasFocus);
161-
eventDelegate.onWindowFocusChanged(hasFocus);
162-
}
163-
164158
@Override
165159
public void onTrimMemory(int level) {
166160
super.onTrimMemory(level);

shell/platform/android/io/flutter/app/FlutterPluginRegistry.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class FlutterPluginRegistry
2828
PluginRegistry.RequestPermissionsResultListener,
2929
PluginRegistry.ActivityResultListener,
3030
PluginRegistry.NewIntentListener,
31-
PluginRegistry.WindowFocusChangedListener,
3231
PluginRegistry.UserLeaveHintListener,
3332
PluginRegistry.ViewDestroyListener {
3433
private static final String TAG = "FlutterPluginRegistry";
@@ -45,7 +44,6 @@ public class FlutterPluginRegistry
4544
private final List<ActivityResultListener> mActivityResultListeners = new ArrayList<>(0);
4645
private final List<NewIntentListener> mNewIntentListeners = new ArrayList<>(0);
4746
private final List<UserLeaveHintListener> mUserLeaveHintListeners = new ArrayList<>(0);
48-
private final List<WindowFocusChangedListener> mWindowFocusChangedListeners = new ArrayList<>(0);
4947
private final List<ViewDestroyListener> mViewDestroyListeners = new ArrayList<>(0);
5048

5149
public FlutterPluginRegistry(FlutterNativeView nativeView, Context context) {
@@ -184,12 +182,6 @@ public Registrar addUserLeaveHintListener(UserLeaveHintListener listener) {
184182
return this;
185183
}
186184

187-
@Override
188-
public Registrar addWindowFocusChangedListener(WindowFocusChangedListener listener) {
189-
mWindowFocusChangedListeners.add(listener);
190-
return this;
191-
}
192-
193185
@Override
194186
public Registrar addViewDestroyListener(ViewDestroyListener listener) {
195187
mViewDestroyListeners.add(listener);
@@ -235,13 +227,6 @@ public void onUserLeaveHint() {
235227
}
236228
}
237229

238-
@Override
239-
public void onWindowFocusChanged(boolean hasFocus) {
240-
for (WindowFocusChangedListener listener : mWindowFocusChangedListeners) {
241-
listener.onWindowFocusChanged(hasFocus);
242-
}
243-
}
244-
245230
@Override
246231
public boolean onViewDestroy(FlutterNativeView view) {
247232
boolean handled = false;

shell/platform/android/io/flutter/embedding/android/FlutterActivity.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -949,14 +949,6 @@ public void onUserLeaveHint() {
949949
}
950950
}
951951

952-
@Override
953-
public void onWindowFocusChanged(boolean hasFocus) {
954-
super.onWindowFocusChanged(hasFocus);
955-
if (stillAttachedForEvent("onWindowFocusChanged")) {
956-
delegate.onWindowFocusChanged(hasFocus);
957-
}
958-
}
959-
960952
@Override
961953
public void onTrimMemory(int level) {
962954
super.onTrimMemory(level);

shell/platform/android/io/flutter/embedding/android/FlutterActivityAndFragmentDelegate.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public boolean onPreDraw() {
581581
void onResume() {
582582
Log.v(TAG, "onResume()");
583583
ensureAlive();
584-
if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
584+
if (host.shouldDispatchAppLifecycleState()) {
585585
flutterEngine.getLifecycleChannel().appIsResumed();
586586
}
587587
}
@@ -629,7 +629,7 @@ void updateSystemUiOverlays() {
629629
void onPause() {
630630
Log.v(TAG, "onPause()");
631631
ensureAlive();
632-
if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
632+
if (host.shouldDispatchAppLifecycleState()) {
633633
flutterEngine.getLifecycleChannel().appIsInactive();
634634
}
635635
}
@@ -652,7 +652,7 @@ void onStop() {
652652
Log.v(TAG, "onStop()");
653653
ensureAlive();
654654

655-
if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
655+
if (host.shouldDispatchAppLifecycleState()) {
656656
flutterEngine.getLifecycleChannel().appIsPaused();
657657
}
658658

@@ -763,7 +763,7 @@ void onDetach() {
763763
platformPlugin = null;
764764
}
765765

766-
if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
766+
if (host.shouldDispatchAppLifecycleState()) {
767767
flutterEngine.getLifecycleChannel().appIsDetached();
768768
}
769769

@@ -898,27 +898,6 @@ void onUserLeaveHint() {
898898
}
899899
}
900900

901-
/**
902-
* Invoke this from {@code Activity#onWindowFocusChanged()}.
903-
*
904-
* <p>A {@code Fragment} host must have its containing {@code Activity} forward this call so that
905-
* the {@code Fragment} can then invoke this method.
906-
*/
907-
void onWindowFocusChanged(boolean hasFocus) {
908-
ensureAlive();
909-
Log.v(TAG, "Received onWindowFocusChanged: " + (hasFocus ? "true" : "false"));
910-
if (host.shouldDispatchAppLifecycleState() && flutterEngine != null) {
911-
// TODO(gspencergoog): Once we have support for multiple windows/views,
912-
// this code will need to consult the list of windows/views to determine if
913-
// any windows in the app are focused and call the appropriate function.
914-
if (hasFocus) {
915-
flutterEngine.getLifecycleChannel().aWindowIsFocused();
916-
} else {
917-
flutterEngine.getLifecycleChannel().noWindowsAreFocused();
918-
}
919-
}
920-
}
921-
922901
/**
923902
* Invoke this from {@link android.app.Activity#onTrimMemory(int)}.
924903
*

shell/platform/android/io/flutter/embedding/android/FlutterFragment.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
import android.content.ComponentCallbacks2;
99
import android.content.Context;
1010
import android.content.Intent;
11-
import android.os.Build;
1211
import android.os.Bundle;
1312
import android.view.LayoutInflater;
1413
import android.view.View;
1514
import android.view.ViewGroup;
16-
import android.view.ViewTreeObserver.OnWindowFocusChangeListener;
1715
import androidx.activity.OnBackPressedCallback;
1816
import androidx.annotation.NonNull;
1917
import androidx.annotation.Nullable;
20-
import androidx.annotation.RequiresApi;
2118
import androidx.annotation.VisibleForTesting;
2219
import androidx.fragment.app.Fragment;
2320
import androidx.fragment.app.FragmentActivity;
@@ -170,19 +167,6 @@ public class FlutterFragment extends Fragment
170167
protected static final String ARG_SHOULD_AUTOMATICALLY_HANDLE_ON_BACK_PRESSED =
171168
"should_automatically_handle_on_back_pressed";
172169

173-
@RequiresApi(18)
174-
private final OnWindowFocusChangeListener onWindowFocusChangeListener =
175-
Build.VERSION.SDK_INT >= 18
176-
? new OnWindowFocusChangeListener() {
177-
@Override
178-
public void onWindowFocusChanged(boolean hasFocus) {
179-
if (stillAttachedForEvent("onWindowFocusChanged")) {
180-
delegate.onWindowFocusChanged(hasFocus);
181-
}
182-
}
183-
}
184-
: null;
185-
186170
/**
187171
* Creates a {@code FlutterFragment} with a default configuration.
188172
*
@@ -1125,23 +1109,9 @@ public void onStop() {
11251109
}
11261110
}
11271111

1128-
@Override
1129-
public void onViewCreated(View view, Bundle savedInstanceState) {
1130-
super.onViewCreated(view, savedInstanceState);
1131-
if (Build.VERSION.SDK_INT >= 18) {
1132-
view.getViewTreeObserver().addOnWindowFocusChangeListener(onWindowFocusChangeListener);
1133-
}
1134-
}
1135-
11361112
@Override
11371113
public void onDestroyView() {
11381114
super.onDestroyView();
1139-
if (Build.VERSION.SDK_INT >= 18) {
1140-
// onWindowFocusChangeListener is API 18+ only.
1141-
requireView()
1142-
.getViewTreeObserver()
1143-
.removeOnWindowFocusChangeListener(onWindowFocusChangeListener);
1144-
}
11451115
if (stillAttachedForEvent("onDestroyView")) {
11461116
delegate.onDestroyView();
11471117
}

shell/platform/android/io/flutter/embedding/engine/FlutterEngineConnectionRegistry.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -732,10 +732,6 @@ private static class FlutterEngineActivityPluginBinding implements ActivityPlugi
732732
private final Set<io.flutter.plugin.common.PluginRegistry.UserLeaveHintListener>
733733
onUserLeaveHintListeners = new HashSet<>();
734734

735-
@NonNull
736-
private final Set<io.flutter.plugin.common.PluginRegistry.WindowFocusChangedListener>
737-
onWindowFocusChangedListeners = new HashSet<>();
738-
739735
@NonNull
740736
private final Set<OnSaveInstanceStateListener> onSaveInstanceStateListeners = new HashSet<>();
741737

@@ -851,25 +847,6 @@ public void removeOnUserLeaveHintListener(
851847
onUserLeaveHintListeners.remove(listener);
852848
}
853849

854-
@Override
855-
public void addOnWindowFocusChangedListener(
856-
@NonNull io.flutter.plugin.common.PluginRegistry.WindowFocusChangedListener listener) {
857-
onWindowFocusChangedListeners.add(listener);
858-
}
859-
860-
@Override
861-
public void removeOnWindowFocusChangedListener(
862-
@NonNull io.flutter.plugin.common.PluginRegistry.WindowFocusChangedListener listener) {
863-
onWindowFocusChangedListeners.remove(listener);
864-
}
865-
866-
void onWindowFocusChanged(boolean hasFocus) {
867-
for (io.flutter.plugin.common.PluginRegistry.WindowFocusChangedListener listener :
868-
onWindowFocusChangedListeners) {
869-
listener.onWindowFocusChanged(hasFocus);
870-
}
871-
}
872-
873850
@Override
874851
public void addOnSaveStateListener(@NonNull OnSaveInstanceStateListener listener) {
875852
onSaveInstanceStateListeners.add(listener);

0 commit comments

Comments
 (0)