在基本(应用程序)模块中显示来自动态功能模块的片段?

Pri*_*alj 2 android android-app-bundle dynamic-feature-module

在我的基本模块(应用程序)中,我有一些片段。我想将其中一个放在动态功能模块中,该模块将按需安装。在用户决定安装这个模块之前,我只会显示一个空的占位符而不是那个 Fragment。我知道如果它是来自动态功能模块的活动很容易,但我真的需要在我的基本模块活动中显示这个片段。

这可能吗?

Côn*_*Hải 5

你可以用这种方式(Kotlin)

// example
val className: String
    get() = "$MODULE_PACKAGE.ui.login.LoginFragment"

fun instantiateFragment(className: String) : Fragment? {
    return try {
        Class.forName(className).newInstance() as Fragment
    } catch (e: Exception) {
        // not install feature module
        null
    }
}
Run Code Online (Sandbox Code Playgroud)