tenten
tenten

Reputation: 1276

Inserting and updating SharedPreferences

I have a help_view that have to show only once when user first open the app when installed. When user uninstall the application and re install it the view must show.

I tried to implement this by shared preferences. See below by code ;

        private void gotoMainActivity() {

        SharedPreferences helpinfo = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = helpinfo.edit();

        boolean help = helpinfo.getBoolean("help", false);

        if(help==false){
            Intent intent = new Intent(this.getApplicationContext(),HelpActivity.class);
            startActivity(intent);

        }else{

            Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
            startActivity(intent);  
        }
    }

I realize I have to update shared preference when first login. Please help me to do this.

Upvotes: 0

Views: 65

Answers (5)

onkar
onkar

Reputation: 4547

         // SharedPreferences
         mSharedpreferences = getApplicationContext().getSharedPreferences("MyPref1", Context.MODE_PRIVATE);

         if (!mSharedpreferences.contains("help")) {
          // Shared preference not present create it. First time launch and set it with default value
          mSharedpreferences.edit().putBoolean("help", true).commit();
          Intent intent = new Intent(this.getApplicationContext(), HelpActivity.class);
          startActivity(intent);

         } else {
          boolean help = helpinfo.getBoolean("help", false);

          if (!help) {
           Intent intent = new Intent(this.getApplicationContext(), HelpActivity.class);
           mSharedpreferences.edit().putBoolean("help", true).commit();

           startActivity(intent);
          } else {

           Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
           startActivity(intent);
          }

         }

Upvotes: 0

Suhas Bachewar
Suhas Bachewar

Reputation: 1230

You have to update code like this:

 private void gotoMainActivity() {

        SharedPreferences helpinfo = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = helpinfo.edit();

        boolean help = helpinfo.getBoolean("help", false);

        if(!help){
            Intent intent = new Intent(this.getApplicationContext(),HelpActivity.class);               
            editor.putBoolean("help",true);
            startActivity(intent);
        }else{

            Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
            startActivity(intent);  
        }
    }

Upvotes: 1

Sohail Zahid
Sohail Zahid

Reputation: 8149

 private void gotoMainActivity() {

        SharedPreferences helpinfo = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        if(!helpinfo.getBoolean("help", false)){

            // First Launch
              helpinfo.edit().putBoolean("help",true).apply();
        }else{

            // Not a first launch
        }
    }

Upvotes: 0

shinil
shinil

Reputation: 1493

try this

 private void gotoMainActivity() {

SharedPreferences helpinfo = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = helpinfo.edit();

boolean help = helpinfo.getBoolean("help", false);

if(!help){
    Intent intent = new Intent(this.getApplicationContext(),HelpActivity.class);
    editor.putBoolean("help",true).commit();
    startActivity(intent);

}else{

    Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
    startActivity(intent);  
}
}

Upvotes: 0

M.Waqas Pervez
M.Waqas Pervez

Reputation: 2430

You should put your boolean in the following code

    if(help==false){
        Intent intent = new Intent(this.getApplicationContext(),HelpActivity.class);
        editor.putBoolean("help",true);
        editor.apply();  
        startActivity(intent);


    }else{

        Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
        startActivity(intent);  
    }

Upvotes: 0

Related Questions