Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity never becomes requested state "[DESTROYED, RESUMED, STARTED, CREATED]" (last lifecycle transition = "PRE_ON_CREATE")

This is not a duplicate question.
I have already looked at similar questions. I have added the questions I have referred to at the last.

My problem is that the test executes, but I have to open the app manually for every test.

screenshot

You can see that I had been waiting for 18 seconds before opening the app. It waits till I open the app manually or it shows this error after a timeout.

Activity never becomes requested state "[DESTROYED, RESUMED, STARTED, CREATED]" (last lifecycle transition = "PRE_ON_CREATE")

From this post, I assume the issue is with the device as I am using Redmi Note 5.
I have disabled animations as well as the MIUI optimization option.

Any help is appreciated to run the tests on Xiaomi devices automatically.

No point in running automated UI tests manually.

Update 1
It executes only for launcher / main activity.
It is not working for other activities.

Update 2
I also happen to have a Redmi 4. The tests work on that device as expected without any code changes.

Referred SO questions

  • AndroidX.Test ActivityScenario: java.lang.AssertionError: Activity never becomes requested state "[RESUMED]" (last lifecycle transition = "STOPPED")
  • Android device doesn't launch activities on the screen while espresso testing
  • Activity never becomes requested state in UI Testing
  • java.lang.AssertionError: Activity never becomes requested state
  • Android instrumented test throws exception: Activity never becomes requested state "[RESUMED, DESTROYED, STARTED, CREATED]"
  • How do I fix this error java.lang.AssertionError: Activity never becomes requested state "[DESTROYED]" (last lifecycle transition = "RESUMED")

And related issues,

  • https://github.com/android/android-test/issues/143
  • https://github.com/android/android-test/issues/496

Adding code snippets and dependencies for anyone who wants to have a look,

Test code

import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4ClassRunner::class)
class MainActivityTest {
    @Test
    fun test_isActivityInView() {
        val activityScenario = ActivityScenario.launch(MainActivity::class.java)
        onView(withId(R.id.layout_activity_main)).check(matches(isDisplayed()))
    }
}

Dependencies

testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test:core:1.4.0"
androidTestImplementation "androidx.test:runner:1.4.0"
androidTestImplementation "androidx.test:rules:1.4.0"
androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
like image 722
Abhimanyu Avatar asked Jul 26 '21 18:07

Abhimanyu


People also ask

How is the process lifecycle tied to the states of activities?

For more information about how the lifecycle of a process is tied to the states of the activities in it, see the Process Lifecycle section of that page. A user expects an activity’s UI state to remain the same throughout a configuration change, such as rotation or switching into multi-window mode.

What is the Order of lifecycle callbacks between two activities?

Rather, the process of starting the second one overlaps with the process of stopping the first one. The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process (app) and one is starting the other. Here's the order of operations that occur when Activity A starts Activity B:

When onsaveinstancestate () is not called?

Note: onSaveInstanceState () is not called when the user explicitly closes the activity or in other cases when finish () is called. To save persistent data, such as user preferences or data for a database, you should take appropriate opportunities when your activity is in the foreground.

How do I navigate between stages of the activity lifecycle?

To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate () , onStart () , onResume () , onPause () , onStop (), and onDestroy (). The system invokes each of these callbacks as an activity enters a new state. Figure 1 presents a visual representation of this paradigm.


4 Answers

This is a problem of permissions. Just allow autostart and pop-up window for this app on your android phone settings.

Then start USB debugging and allow chain-start for the test app.

It worked for me on XiaoMi 11 青春版.

like image 58
zmx0142857 Avatar answered Oct 16 '22 19:10

zmx0142857


I had the same problem. The problem sometimes arises because of using

androidTestImplementation 'androidx.fragment:fragment-testing:1.3.6'

instead of

debugImplementation 'androidx.fragment:fragment-testing:1.3.6'

If you have fragments in your test scenarios add this dependency with debugImplementation and check it again. Thanks to this post for the answer.

like image 44
Mansour Avatar answered Oct 16 '22 20:10

Mansour


I was getting following error on my Android 11 device

java.lang.AssertionError: Activity never becomes requested state "[STARTED, RESUMED, CREATED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")

I wasted a good amount of time on it and found the solution when I switched my device to Android 10.

If anybody facing such issue, please do following things to resolve it,

  1. Enable autostart in app setting
  2. Grant the permission to "Display pop_up windows while running in the background" from app permission settings
like image 3
Pratik Mhatre Avatar answered Oct 16 '22 18:10

Pratik Mhatre


I was testing in a virtual Pixel 2 with API 30. No option for "autostart" or "pop-up" in Settings. In short, what this message says is that can't open the activity, so if you open manually your app, either in virtual device or real one, the test will run.

like image 2
V. Aixalà Avatar answered Oct 16 '22 20:10

V. Aixalà