Reputation: 10049
I am a noob learning android and building a simple game at the same time to face "real world problems" as I learn, the game is a simple maths game where two numbers are given and you have to choose the answer.
This is how I am doing it right now:
My little demo game starts (startscreen.java), click to go to the levels activity
in the levels activity (levels.java) I check the shared prefs for which level has passed (1 to 5) and display the highest, there is also a button to start the game.
Once the game is started (game.java), on 3 wrong answers I save the level to sharedpreferences if this level is higher than before and finish() the game to come back to the levels activity.
My problem is, when I come back to the levels activity now (from the game activity), it does not show the latest level that was just saved in the game activity :(
but if I restart the game or go to startscreen.java and come back it shows.
I can paste the code here if you need me to but I suspect you guys have already solved this :)
Thanks!
Ryan
EDIT: Added onResume, but it still does not work, my code: http://www.pastebin.com/wUUcpMDi
Upvotes: 1
Views: 487
Reputation: 1783
OnCreate() is only called when your activity is created. Try putting the code that reads the sharedpreferences in the OnResume(). See android activity lifecycle.
Upvotes: 1
Reputation: 40218
To ensure that all info will be restored when you get back to your preference screen, you should store these values inside SharedPreference
. Based on your question I assume you're familiar with using it. Just put your latest level value inside SharedPreference
, then fetch it when you get back to the level activity and check the corresponding row. Hope this helps.
Upvotes: 0
Reputation: 1694
Override the onResume method of your levels activity to tell the UI to refresh with the new level information.
Upvotes: 2