Search code examples
androidlibgdxsharedpreferences

Check if app is opened for the first time in LIBGDX


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.


Solution

  • 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();