如何从片段场景获取片段实例

时间:2019-05-06 08:35:19

标签: android android-fragments fragment android-testing androidx

在我的测试用例中,我想获得在片段场景中启动的片段的视图,如下所示:

val scenario = launchFragment<MyFragment>()

MyFragment中,我有一个方法rootView返回该片段的绑定视图。在我的测试中,我想获取根视图并将其保存在另一个字段中。这就是我尝试过的

val rootView: ViewGroup? = null
val scenario = launchFragment<MyFragment>()
scenario.onFragment { fragment -> fragment.rootView {
     rootView = fragment.rootView
  }

如何保存片段场景中的视图?

1 个答案:

答案 0 :(得分:4)

一个较晚的答案,但万一其他人需要它:

val root: View? = null
val scenario = launchFragment<MyFragment>()
scenario.onFragment { fragment ->
    root = fragment.view?.rootView
}
相关问题