Last active
May 17, 2022 05:34
-
-
Save ashley-figueira/4fe9fa329674c6ccd045831916a70910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : Fragment> launchFragmentInHiltContainer( | |
fragmentArgs: Bundle? = null, | |
@StyleRes themeResId: Int = R.style.SomeTheme, | |
fragmentFactory: FragmentFactory? = null, | |
crossinline action: Fragment.() -> Unit = {} | |
) { | |
val startActivityIntent = Intent.makeMainActivity( | |
ComponentName( | |
ApplicationProvider.getApplicationContext(), | |
HiltTestActivity::class.java | |
) | |
).putExtra(EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY, themeResId) | |
ActivityScenario.launch<HiltTestActivity>(startActivityIntent).onActivity { activity -> | |
fragmentFactory?.let { | |
activity.supportFragmentManager.fragmentFactory = it | |
} | |
val fragment: Fragment = activity.supportFragmentManager.fragmentFactory.instantiate( | |
Preconditions.checkNotNull(T::class.java.classLoader), | |
T::class.java.name | |
) | |
fragment.arguments = fragmentArgs | |
activity.supportFragmentManager | |
.beginTransaction() | |
.add(android.R.id.content, fragment, "") | |
.commitNow() | |
fragment.action() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment