android-fragments 片段方案,空片段活动在启动片段容器中不可用

vhipe2zx  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(135)

EmptyFragmentActivity现在是内部活动。
我如何为FragmentScenario测试实现launchFragmentInHiltContainer?

@ExperimentalCoroutinesApi
inline fun <reified T : Fragment> launchFragmentInHiltContainer(
fragmentArgs: Bundle? = null,
themeResId: Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
fragmentFactory: FragmentFactory? = null,
crossinline action: T.() -> Unit = {}
) {
val mainActivityIntent = Intent.makeMainActivity(
    ComponentName(
        ApplicationProvider.getApplicationContext(),
        HiltTestActivity::class.java
    )
).putExtra(FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY, themeResId)

ActivityScenario.launch<HiltTestActivity>(mainActivityIntent).onActivity { activity ->
    fragmentFactory?.let {
        activity.supportFragmentManager.fragmentFactory = it
    }
    val 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 as T).action()
  }
}

当我想实现这个方法来创建一个带注解的Activity以进行刀柄测试时,我遇到了无法访问EmptyFragmentActivity的问题。

yc0p9oo0

yc0p9oo01#

如下面的链接,由谷歌介绍
Goole intereduced link
解决方案链接
我更改此代码

putExtra(FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY, 
themeResId)

用这个

.putExtra("FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY", 
themeResId)

所以新文件看起来像这样

import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import androidx.core.util.Preconditions
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import kotlinx.coroutines.ExperimentalCoroutinesApi

@ExperimentalCoroutinesApi
inline fun <reified T : Fragment> launchFragmentInHiltContainer(
fragmentArgs: Bundle? = null,
themeResId: Int = 
androidx.fragment.testing.
R.style.FragmentScenarioEmptyFragmentActivityTheme,
fragmentFactory: FragmentFactory? = null,
crossinline action: T.() -> Unit = {}
) {
val mainActivityIntent = Intent.makeMainActivity(
    ComponentName(
        ApplicationProvider.getApplicationContext(),
        HiltTestActivity::class.java
    )
).putExtra(
"FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY", 
themeResId)

ActivityScenario.launch<HiltTestActivity>(mainActivityIntent).onActivity { 
activity ->
    fragmentFactory?.let {
        activity.supportFragmentManager.fragmentFactory = it
    }
    val 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 as T).action()
  }
}

相关问题