Make activity/fragment leftover from an attach/detach race slightly safer (flutter/engine#24181)
This commit is contained in:
@@ -532,26 +532,34 @@ public class FlutterActivity extends Activity
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
|
||||
delegate.onStart();
|
||||
if (stillAttachedForEvent("onStart")) {
|
||||
delegate.onStart();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
|
||||
delegate.onResume();
|
||||
if (stillAttachedForEvent("onResume")) {
|
||||
delegate.onResume();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPostResume() {
|
||||
super.onPostResume();
|
||||
delegate.onPostResume();
|
||||
if (stillAttachedForEvent("onPostResume")) {
|
||||
delegate.onPostResume();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
delegate.onPause();
|
||||
if (stillAttachedForEvent("onPause")) {
|
||||
delegate.onPause();
|
||||
}
|
||||
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
|
||||
}
|
||||
|
||||
@@ -591,7 +599,7 @@ public class FlutterActivity extends Activity
|
||||
|
||||
@Override
|
||||
public void detachFromFlutterEngine() {
|
||||
Log.v(
|
||||
Log.w(
|
||||
TAG,
|
||||
"FlutterActivity "
|
||||
+ this
|
||||
@@ -1036,7 +1044,7 @@ public class FlutterActivity extends Activity
|
||||
|
||||
private boolean stillAttachedForEvent(String event) {
|
||||
if (delegate == null) {
|
||||
Log.v(TAG, "FlutterActivity " + hashCode() + " " + event + " called after release.");
|
||||
Log.w(TAG, "FlutterActivity " + hashCode() + " " + event + " called after release.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -624,13 +624,17 @@ public class FlutterFragment extends Fragment implements FlutterActivityAndFragm
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
delegate.onStart();
|
||||
if (stillAttachedForEvent("onStart")) {
|
||||
delegate.onStart();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
delegate.onResume();
|
||||
if (stillAttachedForEvent("onResume")) {
|
||||
delegate.onResume();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(mattcarroll): determine why this can't be in onResume(). Comment reason, or move if
|
||||
@@ -643,7 +647,9 @@ public class FlutterFragment extends Fragment implements FlutterActivityAndFragm
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
delegate.onPause();
|
||||
if (stillAttachedForEvent("onPause")) {
|
||||
delegate.onPause();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -672,7 +678,7 @@ public class FlutterFragment extends Fragment implements FlutterActivityAndFragm
|
||||
|
||||
@Override
|
||||
public void detachFromFlutterEngine() {
|
||||
Log.v(
|
||||
Log.w(
|
||||
TAG,
|
||||
"FlutterFragment "
|
||||
+ this
|
||||
@@ -1118,7 +1124,7 @@ public class FlutterFragment extends Fragment implements FlutterActivityAndFragm
|
||||
|
||||
private boolean stillAttachedForEvent(String event) {
|
||||
if (delegate == null) {
|
||||
Log.v(TAG, "FlutterFragment " + hashCode() + " " + event + " called after release.");
|
||||
Log.w(TAG, "FlutterFragment " + hashCode() + " " + event + " called after release.");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -249,9 +249,16 @@ public class FlutterActivityTest {
|
||||
verify(mockDelegate, times(1)).onDetach();
|
||||
|
||||
flutterActivity.onStop();
|
||||
flutterActivity.onDestroy();
|
||||
|
||||
verify(mockDelegate, never()).onStop();
|
||||
|
||||
// Simulate the disconnected activity resuming again.
|
||||
flutterActivity.onStart();
|
||||
flutterActivity.onResume();
|
||||
// Shouldn't send more events to the delegates as before and shouldn't crash.
|
||||
verify(mockDelegate, times(1)).onStart();
|
||||
verify(mockDelegate, times(1)).onResume();
|
||||
|
||||
flutterActivity.onDestroy();
|
||||
// 1 time same as before.
|
||||
verify(mockDelegate, times(1)).onDestroyView();
|
||||
verify(mockDelegate, times(1)).onDetach();
|
||||
|
||||
@@ -101,9 +101,15 @@ public class FlutterFragmentTest {
|
||||
verify(mockDelegate, times(1)).onDetach();
|
||||
|
||||
fragment.onStop();
|
||||
fragment.onDestroy();
|
||||
|
||||
verify(mockDelegate, never()).onStop();
|
||||
|
||||
fragment.onStart();
|
||||
fragment.onResume();
|
||||
// No more events through to the delegate.
|
||||
verify(mockDelegate, times(1)).onStart();
|
||||
verify(mockDelegate, times(1)).onResume();
|
||||
|
||||
fragment.onDestroy();
|
||||
// 1 time same as before.
|
||||
verify(mockDelegate, times(1)).onDestroyView();
|
||||
verify(mockDelegate, times(1)).onDetach();
|
||||
|
||||
Reference in New Issue
Block a user