|
12 | 12 | import android.view.LayoutInflater; |
13 | 13 | import android.view.View; |
14 | 14 | import android.view.ViewGroup; |
| 15 | +import android.view.ViewTreeObserver.OnWindowFocusChangeListener; |
15 | 16 | import androidx.activity.OnBackPressedCallback; |
16 | 17 | import androidx.annotation.NonNull; |
17 | 18 | import androidx.annotation.Nullable; |
@@ -167,6 +168,16 @@ public class FlutterFragment extends Fragment |
167 | 168 | protected static final String ARG_SHOULD_AUTOMATICALLY_HANDLE_ON_BACK_PRESSED = |
168 | 169 | "should_automatically_handle_on_back_pressed"; |
169 | 170 |
|
| 171 | + private final OnWindowFocusChangeListener onWindowFocusChangeListener = |
| 172 | + new OnWindowFocusChangeListener() { |
| 173 | + @Override |
| 174 | + public void onWindowFocusChanged(boolean hasFocus) { |
| 175 | + if (stillAttachedForEvent("onWindowFocusChanged")) { |
| 176 | + delegate.onWindowFocusChanged(hasFocus); |
| 177 | + } |
| 178 | + } |
| 179 | + }; |
| 180 | + |
170 | 181 | /** |
171 | 182 | * Creates a {@code FlutterFragment} with a default configuration. |
172 | 183 | * |
@@ -1109,12 +1120,21 @@ public void onStop() { |
1109 | 1120 | } |
1110 | 1121 | } |
1111 | 1122 |
|
| 1123 | + @Override |
| 1124 | + public void onViewCreated(View view, Bundle savedInstanceState) { |
| 1125 | + super.onViewCreated(view, savedInstanceState); |
| 1126 | + view.getViewTreeObserver().addOnWindowFocusChangeListener(onWindowFocusChangeListener); |
| 1127 | + } |
| 1128 | + |
1112 | 1129 | @Override |
1113 | 1130 | public void onDestroyView() { |
1114 | 1131 | super.onDestroyView(); |
1115 | 1132 | if (stillAttachedForEvent("onDestroyView")) { |
1116 | 1133 | delegate.onDestroyView(); |
1117 | 1134 | } |
| 1135 | + requireView() |
| 1136 | + .getViewTreeObserver() |
| 1137 | + .removeOnWindowFocusChangeListener(onWindowFocusChangeListener); |
1118 | 1138 | } |
1119 | 1139 |
|
1120 | 1140 | @Override |
@@ -1235,21 +1255,6 @@ public void onUserLeaveHint() { |
1235 | 1255 | } |
1236 | 1256 | } |
1237 | 1257 |
|
1238 | | - /** |
1239 | | - * Called when the current {@link android.view.Window} of the {@link android.app.Activity} that |
1240 | | - * owns this {@link Fragment} gains or loses focus. |
1241 | | - * |
1242 | | - * <p>See {@link android.app.Activity#onWindowFocusChanged(boolean)} |
1243 | | - * |
1244 | | - * @param hasFocus True if the current window has focus. |
1245 | | - */ |
1246 | | - @ActivityCallThrough |
1247 | | - public void onWindowFocusChanged(boolean hasFocus) { |
1248 | | - if (stillAttachedForEvent("onWindowFocusChanged")) { |
1249 | | - delegate.onWindowFocusChanged(hasFocus); |
1250 | | - } |
1251 | | - } |
1252 | | - |
1253 | 1258 | /** |
1254 | 1259 | * Callback invoked when memory is low. |
1255 | 1260 | * |
|
0 commit comments