0

I have an activity and I have a fragment stack in it and sometimes, this stack get's lost... Although I save and restore it...

I know following:

  • onSaveInstanceState is not called:

    a) if the user navigates back, which makes sense...

    b) if the activity is finished

  • onSaveInstanceState is called:

    a) on screen rotation

I discovered through debugging, that I can't be sure, if onSaveInstanceState is called, if screen turns black...

My app is an app, that you use for logging, meaning, you put your phone aside and come back every now and then and fill out your log... So sometimes, my activity is recreated with an empty bundle and onSaveInstanceState was not called, although my app was in the foreground and only the screen turned off...

Questions

1) what can I do to solve that problem? Do I really have to save my fragment states and the stack persistantly?

2) on screen rotation, I can be SURE that onSaveInstanceState is called, can I? Are there any other circumstances, where I can rely on onSaveInstanceState?

prom85
  • 16,896
  • 17
  • 122
  • 242

1 Answers1

1

1) Im reffering to this thread: android life cycle you should use onPause() because you can't be sure if the app is just paused or beeing killed. Android Lifecycle Picture

2) from developer.android.com ( Activity)

Be aware that these semantics will change slightly between applications targeting platforms starting with HONEYCOMB vs. those targeting prior platforms. Starting with Honeycomb, an application is not in the killable state until its onStop() has returned. This impacts when onSaveInstanceState(Bundle) may be called (it may be safely called after onPause() and allows and application to safely wait until onStop() to save persistent state.

Community
  • 1
  • 1
MemLeak
  • 4,456
  • 4
  • 45
  • 84
  • 1
    I would say, Screen rotation and turning the screen off are "in-app" state changes... If I can't use `onSaveInstanceState` in these cases safely, I would be curious, in which cases I can depend on it? Or isn't there any case and all I can do is, save my data in onPause, reget it when needed and clear it manually on demand? – prom85 Mar 11 '14 at 09:44