Search code examples
androidandroid-studioadb

How to kill the app so that it will be started from the last opened activity next time


The app has MainActivity and NextActivity. User opens the app in MainActivity, then goes to NextActivity.

If user sent the app into background the system may kill the application process.

In logs I see that sometimes when user opens the app again next time the system restarts application process from NextActivity instead of MainActivity. Sometimes It causes one crash

I fixed the crash, but to test the fix I have to reproduce scenario when the application process is restarted from the last opened activity.

I checked and tried suggestions from this SOF questions:

Android Simulator: Easy way to simulate a process restart due to low memory?

Simulate killing of activity in emulator

Simulate low battery & low memory in Android

But every time when I kill the application process on next open I saw that the app restarted from MainActivity.


Solution

  • When I need to check such cases I use this adb command:

    adb shell am kill <app_package_name>
    

    and I do it in the following sequence:

    1. Open app screen that I want to check for restoring state.
    2. Move app to background using home button.
    3. Execute the adb command that I mentioned above.
    4. Open app from the recent apps menu.

    If app handles restoring state correctly I see the screen from the first step with correct state if not I see incorrect state of the screen, another screen, crash etc.