杀死应用程序时,SharedPreferences消失

时间:2019-06-16 13:47:33

标签: java android sharedpreferences

我正在尝试使用帐户制作应用,但是我在SharedPrefences上苦苦挣扎。我让他们保存了帐户名称,并且该名称有效,但是只有当我杀死一次应用程序时,我才能关闭手机并重新打开它,并且它仍然存在,但是当我杀死应用程序时却保存了3/4的首选项次,所有帐户都消失了。问题是什么,我用onPauseonDestroyonStop保存首选项,并用onResumeonCreate读取onRestart。在我只有onPause和onResume之前,它的工作原理是相同的。这是代码:

   @Override
protected void onPause(){
    super.onPause();

    sharedpref = this.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpref.edit();
    editor.putString("uzytkownik1", getIntent().getStringExtra("nazwauz"));
    editor.putString("uzytkownik2", getIntent().getStringExtra("nazwauz2"));
    editor.putString("uzytkownik3", getIntent().getStringExtra("nazwauz3"));
    editor.putString("uzytkownik4", getIntent().getStringExtra("nazwauz4"));
    editor.putString("haslo1", getIntent().getStringExtra("haslouzy1"));
    editor.putString("haslo2", getIntent().getStringExtra("haslouzy2"));
    editor.putString("haslo3", getIntent().getStringExtra("haslouzy3"));
    editor.putString("haslo4", getIntent().getStringExtra("haslouzy4"));
    editor.apply();
}

我在onStop和onDestroy中写了同样的内容。

onResume:

        sharedpref = this.getPreferences(Context.MODE_PRIVATE);
    if (sharedpref.getString("uzytkownik1", null) != null || sharedpref.getString("uzytkownik2", null) != null || sharedpref.getString("uzytkownik3", null) != null || sharedpref.getString("uzytkownik4", null) != null) {
        user1.setText(sharedpref.getString("uzytkownik1", null));
        user2.setText(sharedpref.getString("uzytkownik2", null));
        user3.setText(sharedpref.getString("uzytkownik3", null));
        user4.setText(sharedpref.getString("uzytkownik4", null));

,与onCreateonRestart中的相同。

2 个答案:

答案 0 :(得分:2)

设置super.onPause();后尝试放置SharedPreferences

@Override 
protected void onPause(){ 
    sharedpref = this.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpref.edit();
    editor.putString("uzytkownik1", getIntent().getStringExtra("nazwauz"));
    editor.putString("uzytkownik2", getIntent().getStringExtra("nazwauz2"));
    editor.putString("uzytkownik3", getIntent().getStringExtra("nazwauz3"));
    editor.putString("uzytkownik4", getIntent().getStringExtra("nazwauz4"));
    editor.putString("haslo1", getIntent().getStringExtra("haslouzy1"));
    editor.putString("haslo2", getIntent().getStringExtra("haslouzy2"));
    editor.putString("haslo3", getIntent().getStringExtra("haslouzy3"));
    editor.putString("haslo4", getIntent().getStringExtra("haslouzy4"));
    editor.apply(); 

    super.onPause(); 

}

答案 1 :(得分:0)

使用editor.commit()而不是apply()。

根据official docs

  

与commit()不同,后者将其首选项写为持久性   同步存储时,apply()会将其更改提交到内存中   立即使用SharedPreferences,但开始异步提交到   磁盘,并且不会收到任何故障通知。如果另一个编辑器在   这个SharedPreferences做一个常规commit(),而apply()是   仍然未完成,commit()将阻塞,直到所有异步提交都   完成以及提交本身。

当在onPause方法中调用apply()时,它将尝试异步写入数据,但是线程将立即终止,然后再将其写入sharedprefs。您可以使用以下代码替换代码:

@Override
protected void onPause(){
    sharedpref = this.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpref.edit();
    editor.putString("uzytkownik1", getIntent().getStringExtra("nazwauz"));
    editor.putString("uzytkownik2", getIntent().getStringExtra("nazwauz2"));
    editor.putString("uzytkownik3", getIntent().getStringExtra("nazwauz3"));
    editor.putString("uzytkownik4", getIntent().getStringExtra("nazwauz4"));
    editor.putString("haslo1", getIntent().getStringExtra("haslouzy1"));
    editor.putString("haslo2", getIntent().getStringExtra("haslouzy2"));
    editor.putString("haslo3", getIntent().getStringExtra("haslouzy3"));
    editor.putString("haslo4", getIntent().getStringExtra("haslouzy4"));
    editor.commit();

    super.onPause();
}
相关问题