سؤال

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