網(wǎng)上有很多升級到1.12之后的適配問題秃嗜,可以參考 Flutter升級到1.12填坑指南
- 目前的flutter的版本是Flutter 1.17.5 ? channel stable
- 升級后activity的基類使用了io.flutter.embedding.android.FlutterFragmentActivity
- 遇到的坑:程序啟動后直接閃退澎羞,查看log提示:
No view found for id 0x245a3c5c (unknown) for fragment FlutterFragment{id=0x245a3c5c flutter_fragment}
由于不熟悉Flutter妆绞,還以為是我打開Fragment的姿勢不對,于是百度了好久括饶,找到各種打開Fragment的姿勢来涨,最終都沒有解決問題。于是還是自己乖乖的斷點(diǎn)調(diào)試技羔,最終在FlutterFragmentActivity中的onCreate中發(fā)現(xiàn)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
switchLaunchThemeForNormalTheme();
super.onCreate(savedInstanceState);
configureWindowForTransparency();
setContentView(createFragmentContainer());
configureStatusBarForFullscreenFlutterExperience();
ensureFlutterFragmentCreated();
}
private View createFragmentContainer() {
FrameLayout container = new FrameLayout(this);
container.setId(FRAGMENT_CONTAINER_ID);
container.setLayoutParams(
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return container;
}
private void ensureFlutterFragmentCreated() {
FragmentManager fragmentManager = getSupportFragmentManager();
flutterFragment = (FlutterFragment) fragmentManager.findFragmentByTag(TAG_FLUTTER_FRAGMENT);
if (flutterFragment == null) {
// No FlutterFragment exists yet. This must be the initial Activity creation. We will create
// and add a new FlutterFragment to this Activity.
flutterFragment = createFlutterFragment();
fragmentManager
.beginTransaction()
.add(FRAGMENT_CONTAINER_ID, flutterFragment, TAG_FLUTTER_FRAGMENT)
.commit();
}
}
onCreate中會自己設(shè)置contentView,那我之前自己設(shè)置的contentView會把基類的覆蓋鳖粟,ensureFlutterFragmentCreated中添加的fragment會找不到ContainerView 拙绊。所以在自己APP的基類中添加一個ID為FRAGMENT_CONTAINER_ID的view,并設(shè)置為隱藏就OK了
@SuppressLint("ResourceType")
private void addNoVisibleFragment()
{
View view = new FrameLayout(this);
// 這個ID是在FlutterFragmentActivity中定義的數(shù)值
// 在FlutterFragmentActivity中會把view作為contentView
// 但是在咱們自己的APP中會重新設(shè)置contentView
// 所以添加一個隱藏的contentView榄攀,防止由于找不到view導(dǎo)致閃退
view.setId(609893468);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
addContentView(view, params);
view.setVisibility(View.GONE);
}
最后還是鄙視一下Flutter檩赢,搞這么大的坑