mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Support ContextWrapper when FlutterView is instantiated within a Fragment. (flutter/engine#7776)
This commit is contained in:
parent
f583179b54
commit
b07ae25f0a
@ -7,6 +7,7 @@ package io.flutter.view;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.PixelFormat;
|
||||
@ -129,7 +130,11 @@ public class FlutterView extends SurfaceView implements BinaryMessenger, Texture
|
||||
public FlutterView(Context context, AttributeSet attrs, FlutterNativeView nativeView) {
|
||||
super(context, attrs);
|
||||
|
||||
Activity activity = (Activity) getContext();
|
||||
Activity activity = getActivity(getContext());
|
||||
if (activity == null) {
|
||||
throw new IllegalArgumentException("Bad context");
|
||||
}
|
||||
|
||||
if (nativeView == null) {
|
||||
mNativeView = new FlutterNativeView(activity.getApplicationContext());
|
||||
} else {
|
||||
@ -191,6 +196,20 @@ public class FlutterView extends SurfaceView implements BinaryMessenger, Texture
|
||||
sendLocalesToDart(getResources().getConfiguration());
|
||||
sendUserPlatformSettingsToDart();
|
||||
}
|
||||
|
||||
private static Activity getActivity(Context context) {
|
||||
if (context == null) {
|
||||
return null;
|
||||
}
|
||||
if (context instanceof Activity) {
|
||||
return (Activity) context;
|
||||
}
|
||||
if (context instanceof ContextWrapper) {
|
||||
// Recurse up chain of base contexts until we find an Activity.
|
||||
return getActivity(((ContextWrapper) context).getBaseContext());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user