Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6b6afee

Browse files
committed
Switch to listener object instead of ActivityCallThrough
1 parent e6bdea3 commit 6b6afee

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.view.LayoutInflater;
1313
import android.view.View;
1414
import android.view.ViewGroup;
15+
import android.view.ViewTreeObserver.OnWindowFocusChangeListener;
1516
import androidx.activity.OnBackPressedCallback;
1617
import androidx.annotation.NonNull;
1718
import androidx.annotation.Nullable;
@@ -167,6 +168,16 @@ public class FlutterFragment extends Fragment
167168
protected static final String ARG_SHOULD_AUTOMATICALLY_HANDLE_ON_BACK_PRESSED =
168169
"should_automatically_handle_on_back_pressed";
169170

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+
170181
/**
171182
* Creates a {@code FlutterFragment} with a default configuration.
172183
*
@@ -1109,12 +1120,21 @@ public void onStop() {
11091120
}
11101121
}
11111122

1123+
@Override
1124+
public void onViewCreated(View view, Bundle savedInstanceState) {
1125+
super.onViewCreated(view, savedInstanceState);
1126+
view.getViewTreeObserver().addOnWindowFocusChangeListener(onWindowFocusChangeListener);
1127+
}
1128+
11121129
@Override
11131130
public void onDestroyView() {
11141131
super.onDestroyView();
11151132
if (stillAttachedForEvent("onDestroyView")) {
11161133
delegate.onDestroyView();
11171134
}
1135+
requireView()
1136+
.getViewTreeObserver()
1137+
.removeOnWindowFocusChangeListener(onWindowFocusChangeListener);
11181138
}
11191139

11201140
@Override
@@ -1235,21 +1255,6 @@ public void onUserLeaveHint() {
12351255
}
12361256
}
12371257

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-
12531258
/**
12541259
* Callback invoked when memory is low.
12551260
*

0 commit comments

Comments
 (0)