architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

[GithubBrowserSample] ActivityTestRule is deprecated in AutoClearedValueTest

Open CoenQian opened this issue 3 years ago • 0 comments

image

As it mentioned above, ActivityTestRule is deprecated.

Would you please provide a new AutoClearedValueTest by ActivityScenarioRule instead of ActivityTestRule?

I had tried codes like these, but failed.

@get:Rule
val activityScenarioRule = activityScenarioRule<SingleFragmentActivity>()

 @Before
fun setUp() {
    testFragment = TestFragment()
    activityScenarioRule.scenario.onActivity {
        it.setFragment(testFragment)
    }
    InstrumentationRegistry.getInstrumentation().waitForIdleSync()
}

stacktrace error:

java.lang.IllegalStateException: Method addObserver must be called on the main thread
	at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317)
	at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172)

CoenQian avatar Mar 02 '21 08:03 CoenQian