Search code examples
androidsharedpreferenceslifecycleoverwriteonpause

Android lifecycle happening out of order


I am attempting to save keys to SharedPreferences during an onPause() event, but this is called BOTH when I leave the activity and when I return to the activity.

Therefore my values get overwritten with zero and default values when I return to the activity, and my methods in the oncreate method do not respond like I would expect them to because of this.

Perhaps I am misunderstanding the Android lifecycle, but I've seen the lifecycle graph a dozen times and my programs do not act like I would expect them to.

Insight appreciated.


Solution

  • From my experience some devices are completely nuts about the event lifecycle of an activity.

    The only solution I found was to make the activity as independent of the lifecycle as possible. I've seen devices that when they return from onPause destroy the activity only to create a new one. You should probably check if that's not your case.

    The android tests for devices seems to lack a lot in this area.