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.

有帮助吗?

解决方案

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.

其他提示

As per activity life cycle, onPause() will be called both after onResume() and just before onStop(). So anything you do in onPause() will gets execute twice. But in my experiance some devices skipping onPause() before onStop() some times. So you don't rely onPause() alone. Better to write codes on onCreate() or onResume() with proper conditions.

Please put your code here for more clarification.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top