Search code examples
androidcheckboxandroid-activityandroid-lifecycle

Manipulate the checkbox state in Android lifecycle


In my application, my requirement is like I want checkbox to be checked for first time at the time of opening the app and after that I want that value to be retained till the app is unistalled from the device.

For this purpose I have used "Shared Preferences" to store the value on first time check. Till this point everything is fine.

When I press the "home" button and again accessing the app checkbox is in unchecked state. Now for this I have written code to check the checkbox in onResume, but when I close the apps in my system and reopen the app again checkbox is resetting to unchecked. I am not sure how to solve this.

I read the lifecycle of Android here.

After reading this I am assuming to write the checked code in onRestart but again not sure whether the approach is correct or not.


Solution

  • I was able to solve my problem using the shared preferences... Initially I have declared the shared preferences wrong now code was working and also I have used onResume to reinitate the checked value.