0

how can i properly save fragment state, so that its 100% reliable?

My problem is, when i take picture in my fragment, it causes my app to be killed and recreated. State of my fragment is lost.

I have tried onSaveInstanceState which to my surprise didnt work, if app is killed after taking picture, state is lost onCreate(Bundle savedInstanceState) savedInstanceState is null...

I have tried having state variables as static. Firstly, i dont like this solution, secondly it dont work in my special case since i have multiple instances of fragment class across application.

What is recommended approach?

hendrix
  • 3,364
  • 8
  • 31
  • 46

1 Answers1

1

Maybe you should consider using SharedPreferences. Have a look at this question. How to use SharedPreferences in Android to store, fetch and edit values

Or you can create an Sqlite database to persist your state. Since you have custom classes I'm guessing thats what you will have use.

http://www.vogella.com/articles/AndroidSQLite/article.html

I don't recommend using static variables in your app because they can get flushed if the system is low on memory.

Community
  • 1
  • 1
Marco RS
  • 8,145
  • 3
  • 37
  • 45