2

I am trying to check if the app is opened for first time in LIBGDX. I want this logic to be implemented in level selection screen. I want the sprite to get different for the first time. I have implemented the below code,

    private static Preferences prefs;
    public MenuScreen(MyGame game) {
         prefs = Gdx.app.getPreferences("firsttimeopen");
            if (prefs.getBoolean("lock",true) ) {
            prefs.putBoolean("lock", false);
            Gdx.app.log("firsttimeopening" + a, "firsttimeopening" + a);
             } else {
        Gdx.app.log("secondtimeopening" + a, "secondtimeopening" + a);
           }

Here both the condition becomes true when i open the app.I don't know where did I go wrong. I even referred this question stackoverflow and even this question stackoverflowbut nothing helped. Help me. Thanks in advance.

Community
  • 1
  • 1
Anusha
  • 939
  • 3
  • 13
  • 31
  • did you forget to add SharedPreferences.Editor editor = sharedpreferences.edit(); prefs.putBoolean("lock", false); editor.commit(); – saeed Oct 31 '16 at 15:01
  • It throws error saying "cannot resolve symbol SharedPreferences" @saeed – Anusha Oct 31 '16 at 15:02
  • I tried changing it to Preferences from SharedPreferences but it throws error saying "cannot resolve symbol editor" @saeed – Anusha Oct 31 '16 at 15:03
  • @saeed, you're wrong since @anusha is using `libgdx`. Check the answer to find the problem – Iulian Popescu Oct 31 '16 at 15:08

1 Answers1

3

From the wiki:

Your changes to a preferences instance will only get persisted if you explicitly call the flush() method.

Add the line in your code:

prefs.putBoolean("lock", false);
prefs.flush();
wmora
  • 1,203
  • 1
  • 9
  • 17