• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to add shared preferences?

 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a tic tac toe game app created in a table layout with 9 buttons in three rows for the game, and a new game button at the end.
I want to add shared preferences to the code so the game doesn't keep restarting. I know i need an onPause and onResume method but I am not sure what goes in the methods

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Wilson wrote:I have a tic tac toe game app created in a table layout with 9 buttons in three rows for the game, and a new game button at the end.
I want to add shared preferences to the code so the game doesn't keep restarting. I know i need an onPause and onResume method but I am not sure what goes in the methods

Hm. I don't think I understand the connection. Why does your game keep restarting? What does 'keep restarting' mean? Why do you think shared preferences will help?
 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean restarting when you change orientation? For that case you should be saving your application state when onSaveInstanceState(..) is called, and checking-for and using any saved information when onCreate(..) is called.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the game restarts when i change orientation
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe it won't fix the orientation problem but i was told to include shared preferences for this so could someone help please?
 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This page talks about Using Shared Preferences and other options for persistent storage, but they are not intended to be used for temporarily storage of application data during events like an orientation change.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should be used to save the values during orientation change?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bundles and custom objects. This might be the basis of your code saving and restoring state:

Personally, I've found storing state to shared preferences preferable, as the above methods did not work reliably for me. I could have been doing something wrong, of course, and those are the recommended methods.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I've found storing state to shared preferences preferable

ok how would I do that?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by reading the article Ron linked to. You'd store state in onPause , and restore it in onResume.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic