2016-06-08 13 views
0

Ich habe eine help_view, die nur einmal angezeigt werden muss, wenn der Benutzer die App zum ersten Mal öffnet, wenn er installiert ist. Wenn der Benutzer die Anwendung deinstalliert und neu installiert, muss die Ansicht angezeigt werden.Einfügen und Aktualisieren von SharedPreferences

Ich habe versucht, dies durch gemeinsame Einstellungen zu implementieren. Siehe unten nach 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); 
     } 
    } 

Ich weiß, ich geteilt Präferenz aktualisieren muß, wenn die erste Anmeldung. Bitte helfen Sie mir, dies zu tun.

Antwort

0

Sie sollten Ihre boolean im folgenden Code setzen

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

     } 
0

versuchen diese

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

Sie haben Code wie folgt zu aktualisieren:

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