2013-03-03 2 views
8

에 된 SharedPreferences를 잊고 내가 응용 프로그램은 다시 시작

은 안드로이드 3.0 이상에서 잘 동작하는 것 더 이상하지만, 안드로이드 2.3.3에 때마다 나는 그것이 응용 프로그램을 실행 그것으로 무엇을 해야할지하지 않습니다 유지 사용자 이름/암호를 다시 묻습니다.

공유 환경 설정을 사용하고 있습니다. 여기

내가 환경 설정 저장하는 방법입니다

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putString("username", username).commit(); 
     editor.putString("password", password).commit(); 

:

SharedPreferences preferences = MyApplication.getAppContext().getSharedPreferences("athopbalance", Context.MODE_PRIVATE); 
    String username = preferences.getString("username", ""); 
    String password = preferences.getString("password", ""); 

나는이 코드를 사용하여 환경 설정을 저장하려고 여기

 SharedPreferences preferences = MyApplication.getAppContext().getSharedPreferences("athopbalance", MODE_PRIVATE); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putString("username", username).commit(); 
     editor.putString("password", password).commit(); 

과 내가 그들을 읽는 방법입니다 그리고이 코드를 읽어보십시오 :

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MyApplication.getAppContext()); 
    String username = preferences.getString("username", ""); 
    String password = preferences.getString("password", ""); 

하지만 작동하지 않습니다.

문제는 응용 프로그램을 다시 시작하기 전에 문제가 계속 남아 있다는 것입니다. 그러나 내가 다시 시작하자마자 사용자 이름을 ""(빈 문자열)로, 비밀번호를 ""가져 오는 것으로 끝납니다.

어떤 아이디어가 크게

답변

0

정확히 어떤 일이 일어 났는지는 알 수 없지만, 에뮬레이터 문제를 다시 시작한 후에는 어떻게되었는지 알 수 없습니다.

죄송합니다

당신은 두 가지 옵션이 있습니다 시간

+0

여기에서 같은 문제가 발생하며 재시동 후 사라집니다. 정말 이상한 ... – Victor

1

을 감상 할 수있다보십시오이 :

상황이 앱 맥락이다
SharedPreferences preferences = context.getSharedPreferences("YOUR_TAG", 0); 
String username = preferences.getString("username", ""); 
String password = preferences.getString("password", ""); 


    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString("username", username).commit(); 
    editor.putString("password", password).commit(); 

:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


     context = this; 
+0

안녕하세요, 당신의 의견 주셔서 대단히 감사합니다. 내 첫 번째 예제에서 꽤 똑같은 일을하고 있지만, 작동하지 않는 것 같아요. –

+0

MyApplication 대신 활동을 시도해보십시오. – Gyonder

+0

활동의 컨텍스트를 전달하는 대신 getApplicationContext()를 호출했습니다. –

2
public class MyApplication extends Application { 
    private static MyApplication instance; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.d(TAG, "onCreate() called"); 
    } 

    public MyApplication() { 
     super(); 
     Log.d(TAG, "Constructor called"); 
     instance = this; 
    } 


    public static MyApplication getApplication() { 
     if (instance == null) { 
      instance = new MyApplication(); 
     } 
     return instance; 
    } 
} 

그리고 사용 :

MyApplication.getApplication().getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE) 
+0

당신의 예제는 리턴하기 전에'new MyApplication'을 인스턴스에 저장하지 않으므로 캐싱은 결코 일어나지 않습니다. – mikebabcock

+0

감사합니다. –

+0

mikebabcock이 지적한 문제를 해결했습니다. 감사! – agamov

관련 문제