2016-06-08 4 views
0

사용자가 처음 설치했을 때 한 번만 열어야하는 help_view가 있습니다. 사용자가 응용 프로그램을 제거하고 다시 설치하면보기가 표시되어야합니다.SharedPreferences 삽입 및 업데이트

공유 환경 설정으로 구현하려고했습니다. 아래 코드를 참조하십시오.

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

처음 로그인 할 때 공유 환경 설정을 업데이트해야한다는 것을 알고 있습니다. 도와주세요.

답변

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

시도 다음 코드에서 부울을 넣어해야 당신은 다음과 같은 코드를 업데이트해야이

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

:

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