开发者

Android, reading preferences

开发者 https://www.devze.com 2023-04-04 13:32 出处:网络
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

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 开发者_StackOverflowis, 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


Override the onResume method of your levels activity to tell the UI to refresh with the new level information.


OnCreate() is only called when your activity is created. Try putting the code that reads the sharedpreferences in the OnResume(). See android activity lifecycle.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消