Created
March 16, 2020 03:08
-
-
Save emmano/16514b1bd56b96a0961ca5db89ff3ed1 to your computer and use it in GitHub Desktop.
launchInActivity.kt
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 F : Fragment, reified A : AppCompatActivity> launchInActivity( | |
crossinline fragmentFactory: () -> F, | |
state: Lifecycle.State = Lifecycle.State.RESUMED, | |
layoutId: Int = android.R.id.content, | |
isTestOnlyActivity: Boolean = false | |
) : ActivityScenario<A> { | |
//Manually register test only Activity since manifest merging is broken for test resources https://github.com/robolectric/robolectric/issues/4725 | |
if(isTestOnlyActivity) { | |
val appContext = ApplicationProvider.getApplicationContext<Application>() | |
val activityInfo = ActivityInfo().apply { | |
name = A::class.java.name | |
packageName = appContext.packageName | |
} | |
shadowOf(appContext.packageManager).addOrUpdateActivity(activityInfo) | |
} | |
val activityScenario = ActivityScenario.launch(A::class.java) | |
activityScenario.onActivity { | |
val fragment = fragmentFactory() | |
it.supportFragmentManager | |
.beginTransaction() | |
.replace(layoutId, fragment, F::class.java.name) | |
.setMaxLifecycle(fragment, state) | |
.commitNow() | |
} | |
return activityScenario | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment