Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Fragment from Dynamic Feature Module in base (app) module?

In my base module (app) I have a few Fragments. I would like to put one of them in a Dynamic Feature Module, which would get installed on-demand. Until the user decides to install this module, I would just show an empty placeholder instead of that Fragment. I know it's easy if it's an Activity from Dynamic Feature Module, but I would really need to show this Fragment in my base module Activity.

Is this possible?

like image 961
c0dehunter Avatar asked Sep 21 '25 00:09

c0dehunter


1 Answers

You can use this way (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
    }
}
like image 72
Công Hải Avatar answered Sep 22 '25 21:09

Công Hải