SharedPreference does not work as expected

I have an activity that I only want to run when the application launches for the first time. I checked the highlighted shared preference, which returns a boolean. If it returns true, it will be launched, and it will be set to false, so that it will not start the next time the application is opened. But my implementation went wrong. Every time I open, BeforMain1 activity becomes open. Can someone tell me what is wrong in my code?

sharedPreferences = getSharedPreferences("ShaPreferences", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            boolean  firstTime=sharedPreferences.getBoolean("first", true);
            if(firstTime) {
                editor.putBoolean("first",false);
                Intent intent = new Intent(SplashScreen.this, BeforeMain1.class);
                startActivity(intent);
            }
            else
            {
                Intent intent = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(intent);
            }
+4
source share
2 answers

You forgot to commit changes to SharedPreferences,

if(firstTime) {
      editor.putBoolean("first",false);
      //For commit the changes, Use either editor.commit(); or  editor.apply();.
      editor.commit();  or  editor.apply();
      Intent intent = new Intent(SplashScreen.this, BeforeMain1.class);
      startActivity(intent);
}else {
      Intent intent = new Intent(SplashScreen.this, MainActivity.class);
      startActivity(intent);
}
+5
source

, , .

SharedPreferences validation;
SharedPreferences.Editor editorValidation;


validation = getSharedPreferences("myShaPreferences", Context.MODE_PRIVATE);

if(validation.contains("firsttime"))
{
        Intent intent = new Intent(getApplicationContext(), MainActivity.class); //create intent with second screen
                startActivity(intent); //call second screen

        /* 
        //this code you will put in your application but is redundant... with the else and comprobation if the firsttime is true or false
         if(Validation.getBoolean("firsttime", false))
        {
            Intent intent = new Intent(SplashScreen.this, MainActivity.class); //create intent with second screen
            startActivity(intent); //call second screen
        }else{
            //if the sharedpreference firsttime is true then call the first screen but it is redundant
            Intent intent = new Intent(getApplicationContext(), BeforeMain1.class); //create var intent with the screem for first time
            startActivity(intent); //call new screen
        }
        */
}else{
    //put the sharedpreference boolean firsttime equal to false
    editorValidation= validation.edit();  // edit the sharedpreference
    editorValidation.putBoolean("firsttime",false); // put false to firsttime
    editorValidation.apply(); //apply(); runs in background, no return anything and commit(); return true or false

    //call screen before the main - for the first time
    Intent intent = new Intent(getApplicationContext(), BeforeMain1.class); //create var intent with the screem for first time
        startActivity(intent); //call new screen
}
0

Source: https://habr.com/ru/post/1613983/


All Articles