How one can Simulate Course of Dying in Android?


There are 2 sorts of course of loss of life in Android:

  • System-initiated Course of Dying
  • Person-initiated Course of Dying

System-initiated course of loss of life means the method is killed by the Android working system. It may very well be on account of useful resource constraints (not sufficient reminiscence) or just configuration adjustments (display rotation).

Person-initiated course of loss of life means the method is killed by the person. For instance, you click on the house button, and manually shut the background app.

Normally as a developer, you care about solely the system-initiated course of loss of life since you’re required to save lots of the UI state. For user-initiated course of loss of life, you do NOT want to save lots of the UI states.

System-initiated Course of Dying Person-initiated Course of Dying
Required to save lots of UI states NOT required to save lots of UI states

For saving UI states expectation, you’ll be able to seek advice from the official documentation right here.

System-initiated Course of Dying

1. Use Logcat Terminate App Button

The simplest option to simulate system-initiated course of loss of life, use the Logcat terminate app button.

  • You press the house button.
  • Then, use the Logcat terminate app button as proven beneath.

Activity_and_ViewModel_Lifecycle_Demo_App_01.png

Wait, this “Terminate App” button has been REMOVED in Android Studio Dolphin. See the problem tracker right here.

Based on the ticket, it has been moved to Gadget Monitor in Android Studio Flamingo model. I simply tried, it does NOT work! It might probably NOT be used to interchange this Logcat Terminate App Button!

Drive Cease in Gadget Monitor != Logcat Terminate App

In gadget monitor (Android Studio Flamingo), there are 2 choices

  • Kill course of – equal to adb shell am kill command
  • Drive cease course of – equal to adb shell am force-stop command

How to Simulate Process Death in Android 001.png

Kill course of does nothing as a result of the gadget just isn’t rooted.

Drive cease course of does kill the method, BUT it does NOT simulate system-initiated course of loss of life. It simulates user-initiated course of loss of life, which is strictly the identical as utilizing adb shell am force-stop command – see beneath.

So, this isn’t what we wish. Even in the event you improve to this Android Studio Flamingo model, you will not in a position to make use of Gadget Monitor to simulate system-initiated course of loss of life.

2. Set No Background Processes – Developer Choices

The one workaround that I’m conscious is about no background processes in developer choices. To allow developer choices, you seek advice from the step right here.

  • Go to Setting -> System -> Developer Choices (depends upon Telephone, Developer Choices may very well be at totally different location)
  • In Apps, Background course of restrict, choose No background processes

How to Simulate Process Death in Android 00.png

To simulate system-initiated course of loss of life, after getting launched your app

  • Press the Residence button
  • Launch different app and press the Residence button

Your app course of will probably be killed. It’s best to be capable to see that in your Logcat in the event you’re utilizing Android Studio Dolphin.

PROCESS ENDED (8227) for package deal com.instance.understandlifecyclesdemo

You can even examine the method utilizing the adb shell ps in Android Studio terminal and ensure your app package deal is not proven up within the course of checklist.

I simply tried this technique and it would not appear to work on API Stage 33. The method continues to be on the background. It labored on API stage 26 a minimum of (the API stage that I’ve tried sucessfully).

Person-initiated Course of Dying

The simplest option to simulate user-initiated course of loss of life is doing precisely what person can do.

1. Manually Shut the App

  • Press the Residence button
  • Press the Sq. button
  • Shut the App – press the X or CLEAR ALL

How to Simulate Process Death in Android 01.gif

2. adb shell am force-stop

One other approach is utilizing the adb command within the terminal. You possibly can run this command even your app is in foreground. It’s going to simply kill your app immediately.

adb shell am force-stop <applicationId>

The applicationId is the one you outline within the construct.gradle app stage. Please word, it’s NOT your package deal title.

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.instance.understandlifecyclesdemo"

        
    }
}

So the complete command appears like this based mostly on the above applicationId title.

adb shell am force-stop com.instance.understandlifecyclesdemo

Just like above, you’ll be able to verify whether or not the method has been efficiently killed utilizing this command adb shell ps.

I additionally attempt adb shell am kill <applicationId>. It would not work for me. Nothing occurs, no errors. I believe it requires your gadget to be rooted.

Course of Dying Restoration

Whereas taking part in with saving UI states, I discover for user-initiated course of loss of life restoration, Android OS does NOT restore the UI states even you save information into the bundle earlier than your app is killed. Particularly, Exercise.onRestoreInstanceState() just isn’t known as. In Exercise.onCreate(), the Bundle parameter is null.

For system-initiated course of loss of life restoration, Android OS restores the UI states if there are information being saved previous to course of loss of life. Particularly, Exercise.onRestoreInstanceState() known as. In Exercise.onCreate(), the Bundle parameter is NOT null.

That is much like SavedStateHandle in your view mannequin. For user-initiated course of loss of life restoration, retrieved worth from SavedStateHandle is null even you save the information previous to that. For system-initiated course of loss of life restoration, the retrieved worth is the beforehand saved worth.

Right here is the abstract

System-initiated Course of Dying Restoration Person-initiated Course of Dying Restoration
onRestoreInstanceState() known as onRestoreInstanceState()is NOT known as
Bundle is NOT null in onCreate() Bundle is null in onCreate()
SavedStateHandle.get() returns beforehand saved worth SavedStateHandle.get() returns null

SavedStateHandle in view mannequin is seperated from the Bundle within the exercise. No matter being saved from the Bundle can NOT be retrieved from SavedStateHandle.

Conclusion

If you happen to’re on Android Studio Dolphin or later, the one option to simulate system-initiated course of loss of life is Set No Background Processes in Developer Choices, however by some means it would not work on API stage 33. So the workaround is to make use of API stage 26. It might work on different API ranges.

Supply Code

GitHub Repository: Demo_UnderstandLifecycles

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles