插入和更新SharedPreferences

时间:2016-06-08 05:06:03

标签: android sharedpreferences

我有一个help_view,当用户在安装时首次打开应用时,只需显示一次。当用户卸载应用程序并重新安装时,视图必须显示。

我尝试通过共享首选项实现此功能。见下面的代码;

        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);  
        }
    }

我意识到首次登录时必须更新共享首选项。 请帮我这样做。

5 个答案:

答案 0 :(得分:1)

您必须更新以下代码:

 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);  
        }
    }

答案 1 :(得分:0)

您应该将布尔值放在以下代码中

    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);  
    }

答案 2 :(得分:0)

         // 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);
          }

         }

答案 3 :(得分:0)

试试这个

 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);  
}
}

答案 4 :(得分:0)

 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
        }
    }