android - 将 FlutterFragment 添加到 Activity?

标签 android flutter

我正在尝试将 flutter 应用程序添加为原生 android Activity 中的 fragment 。 对于 flutter,我使用了默认的计数器应用程序 在 native Android 方面,我使用了默认抽屉导航模板

我正在尝试在用户点击抽屉导航中的图库磁贴时添加 flutter 应用

class GalleryFragment : FragmentActivity() {
companion object {
    // Define a tag String to represent the FlutterFragment within this
    // Activity's FragmentManager. This value can be whatever you'd like.
    private const val TAG_FLUTTER_FRAGMENT = "flutter_fragment"
}

    // Declare a local variable to reference the FlutterFragment so that you
    // can forward calls to it later.
    private var flutterFragment: FlutterFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Inflate a layout that has a container for your FlutterFragment. For
        // this example, assume that a FrameLayout exists with an ID of
        // R.id.fragment_container.
        setContentView(R.layout.content_main)

        // Get a reference to the Activity's FragmentManager to add a new
        // FlutterFragment, or find an existing one.
        val fragmentManager: FragmentManager = supportFragmentManager

        // Attempt to find an existing FlutterFragment, in case this is not the
        // first time that onCreate() was run.
        flutterFragment = fragmentManager
            .findFragmentByTag(TAG_FLUTTER_FRAGMENT) as FlutterFragment?

        // Create and attach a FlutterFragment if one does not exist.
        if (flutterFragment == null) {
            var newFlutterFragment = FlutterFragment.withCachedEngine("my_engine").build<FlutterFragment>()
            flutterFragment = newFlutterFragment
            fragmentManager
                .beginTransaction()
                .add(
                    R.id.nav_host_fragment,
                    newFlutterFragment,
                    TAG_FLUTTER_FRAGMENT
                )
                .commit()
        }
    }
}

当我尝试使用 fragment 管理器添加 FlutterFragment 时出现此错误。 Fragment 和 FlutterFragment 不匹配。

最佳答案

根据文档,将 Flutter 添加到 Fragment 仍然是实验性的。有关详细信息,请阅读:https://github.com/flutter/flutter/wiki/Experimental:-Add-Flutter-Fragment

关于android - 将 FlutterFragment 添加到 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59352374/

相关文章:

android - 从通知中发送 ACTION_SEND Intent

http - flutter 如何处理 http 服务器端验证错误

android - 在 M1 Apple Silicon 上更新 Android Studio 北极狐(2020.3.1)后,找不到与 flutter 医生 bundle 的 Java 版本

dart - 使用 Visual Studio Code 在 Flutter 上运行 Gradle 时出错

flutter - 在 flutter 中滚动 ListView 时,网络图像不断消失?

java - 使用 Java 仅更新现有 Google Drive 文件的元数据

android - TestScheduler 不起作用(Kotlin + RxJava2 + mockito)

java - 将圆点放入java中使用的圆弧

java - 无需打开浏览器的 HTTP 请求 - ANDROID

使用 getx 传递多个数据