1

我有一个供客户端使用的 android SDK,SDK 有一些抽象,因此客户端不必处理颤振绑定。

我的 SDK 我有这样的东西


class SDKPrivate  {
private static FlutterEngine flutterEngine;
...
 public void initFlutterEngine(Context context){
        if(SDKPrivate.flutterEngine != null) return;

        SDKPrivate.flutterEngine = new FlutterEngine(context);

        SDKPrivate.flutterEngine.getDartExecutor()
                .executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
    }

    MyFragment createFragment() {
        return new MyFragment();
    }
...
}

片段类是

public class MyFragment extends FlutterFragment {

    @Override
    public boolean shouldAttachEngineToActivity() {
        return true;
    }

     @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated( savedInstanceState);
        GeneratedPluginRegistrant.registerWith(sdkPrivate.getEngine());
    }

    @Override
    @Nullable
    public FlutterEngine provideFlutterEngine(@NonNull Context context) {
        return sdkPrivate.getEngine();
    }

在我为进行测试而创建的示例应用程序中,我有一个主要活动

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SDKPrivate.initFlutterEngine(getApplicationContext());

它有一个按钮,调用我的新活动,里面有 MyFragment

Intent activity2Intent = new Intent(getApplicationContext(), FragActivity.class);
            startActivity(activity2Intent);

FragActivity 是这样的

public class FragActivity extends AppCompatActivity  {

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_frag);

       MyFragment  frag = SDKPrivate.createFragment();

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.frame_layout, frag,  BuildConfig.APPLICATION_ID + ".MyFragment")
                .commit();
    }
}

当我输入 FragActivity 时有这样的代码时,应用程序保持空白。但是 ,如果我在 FragActivity 中启动引擎,则应用程序会按预期工作。

这样就好了

public class FragActivity extends AppCompatActivity  {

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_frag);
        SDKPrivate.initFlutterEngine(getApplicationContext());
       //Same code as showed above 
      ...
}

是否有一个原因?我还想为客户提供一种方法来预热引擎,然后在片段和活动上使用它而不创建新的。

颤振医生-v

[✓] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.15.1 19B2093, locale en-PT)
    • Flutter version 1.12.13+hotfix.5 at /Users/manuelpeixoto/flutter
    • Framework revision 27321ebbad (6 weeks ago), 2019-12-10 18:15:01 -0800
    • Engine revision 2994f7e1e6
    • Dart version 2.7.0


[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/manuelpeixoto/Library/Android/sdk/
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = /Users/manuelpeixoto/Library/Android/sdk/
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.2.1, Build version 11B500
    • CocoaPods version 1.8.4

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 42.0.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.41.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.7.1

[✓] Connected device (2 available)
    • XT1097                   • 0016256677                • android-arm • Android 6.0 (API 23)
    • iPhone  • 00008030-000A053901D0802E • ios         • iOS 13.3

• No issues found!
4

2 回答 2

0

一旦你创建了一个引擎缓存它供以后使用。// 缓存预热的 FlutterEngine 以供 FlutterFragment 稍后使用。FlutterEngineCache .getInstance() .put("my_engine_id", flutterEngine);

并在颤振片段中使用它作为 flutterFragment.withCachedEngine("my_engine_id").build();

于 2020-01-22T19:36:57.460 回答
0

我能够解决问题,我的 main.dart 中有这条线

await SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])

由于某种原因,这使得颤振初始化停止。

我把它改成,

 unawaited(SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]))

它按预期工作。

于 2020-01-23T12:07:56.450 回答