我正在尝试将 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/