Possible Duplicate:
variables retaining values after app close
I am relatively new to android and java, so I am sorry if this a stupid question. I have an android application that allows a user to take pictures and save them. I am saving the pictures as Attachment 0, Attachment 1 etc.
I am using an integer to decide the attachment number and this int gets incremented every time an attachment is created. my photos are getting saved properly with the proper names, but when I close my application the value of the integer gets reset to 0 and the already saved attachments get overwritten. How do I prevent the integer from resetting on closing and opening the application ? Thanks !
Use
SharedPreferencesto save theintvalue, then reload it inonCreate.There is an excellent bit of code here for you to learn from, and I’ll give you a bit of pseudocode to get you going:
And that should do it! That’s the basic concept of
SharedPreferences, and from that, I think you’ll have no problem adapting the tutorials above to your purpose.