2015-12-01 49 views
1
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    layout = (LinearLayout) findViewById(R.id.main); 
    if (savedInstanceState != null) { 
     int counter = PreferenceManager.getDefaultSharedPreferences(this).getInt("key2", 0); 
     for (int i = 0; i < counter; i++) { 
      layout.addView(createToDo(PreferenceManager.getDefaultSharedPreferences(this).getString(Integer.toString(counter), "")), counter); 
     } 
    } 
@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState){ 
    LinearLayout layout = (LinearLayout) findViewById(R.id.todoMain); 
    super.onRestoreInstanceState(savedInstanceState); 
    textList = savedInstanceState.getStringArrayList("key"); 
    for (String t: textList) { 
     todoList.add(createToDo(t)); 
     layout.addView(createToDo(t)); 
    } 
} 

@Override 
protected void onSaveInstanceState(Bundle savedInstanceState){ 
    super.onSaveInstanceState(savedInstanceState); 
    savedInstanceState.putStringArrayList("key", textList); 
} 

protected void onPause() { 
    super.onPause(); 
    int counter = 0; 
    for (String t: textList) { 
     getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putString(Integer.toString(counter), t); 
     counter++; 
    } 
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().commit(); 
    SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE); 
    SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit(); 
    editor2.putInt("key2", counter); 
    editor2.commit(); 
} 

protected void onResume() { 
    super.onResume(); 
    layout = (LinearLayout) findViewById(R.id.todoMain); 
    int counter = PreferenceManager.getDefaultSharedPreferences(this).getInt("key2", 0); 
    for (int i = 0; i < counter; i++) { 
     layout.addView(createToDo(PreferenceManager.getDefaultSharedPreferences(this).getString(Integer.toString(i), "")), i); 
    } 
} 

protected void onDestroy() { 
    super.onDestroy(); 
    int counter = 0; 
    for (String t: textList) { 
     getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putString(Integer.toString(counter), t); 
     counter++; 
    } 
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().commit(); 
    SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE); 
    SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit(); 
    editor2.putInt("key2", counter); 
    editor2.commit(); 
} 

所以,當我按我的Android設備上的後退按鈕或退出應用程序,它似乎總是重置應用程序,並刪除所有文字意見。如何保存我的文字瀏覽,以便在按下或退出應用程序時再次顯示它們?謝謝。後退按鈕刪除我所有的textviews

回答

2

您是否在更新後提交SharedPreferences?

看看下面的例子。

SharedPreferences sharedPreferencesSettings = context.getSharedPreferences("sharedPreferencesSettings", Context.MODE_PRIVATE); 

    SharedPreferences.Editor editor = sharedPreferencesSettings.edit(); 

    editor.putBoolean("showRedColor", true); 

    editor.commit(); 

嘗試修改下面的方法。

protected void onPause() { 
    super.onPause(); 
    int counter = 0; 
    //get an editor 
    SharedPreferences.Editor editor1 = getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit() 
    for (String t: textList) { 
     //set key values 
     editor1.putString(Integer.toString(counter), t).commit(); 
     counter++; 
    } 
    //save changes 
    editor1.commit(); 
    SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE); 
    SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit(); 
    editor2.putInt("key2", counter); 
    editor2.commit(); 
} 

protected void onDestroy() { 
    super.onDestroy(); 
    int counter = 0; 
    //get an editor 
    SharedPreferences.Editor editor1 = getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit() 
    for (String t: textList) { 
     //set key values 
     editor1.putString(Integer.toString(counter), t).commit(); 
     counter++; 
    } 
    //save changes 
    editor1.commit(); 
    SharedPreferences sharedPreferencesSettings2 = this.getSharedPreferences("PREFERENCE2", MODE_PRIVATE); 
    SharedPreferences.Editor editor2 = sharedPreferencesSettings2.edit(); 
    editor2.putInt("key2", counter); 
    editor2.commit(); 
} 

在你的例子中,你沒有保存在正確的編輯器中。

for (String t: textList) { 
    //Get editor, set the key, but you are not committing the changes, so they are lost 
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putString(Integer.toString(counter), t); 
    counter++; 
} 
//Get a new editor and you are committing, but you haven't edit any of the key value, so you are not commint any changes 
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().commit(); 
+0

我在提交中添加,它似乎仍然無法正常工作。 – dk3512

+0

我已更新我的答案,請嘗試更新onPause方法和OnDestroy –

0

您沒有在SharedPreferences編輯器中調用commit(),因此沒有任何內容正在保存。

編輯:另外,如果你的目標是棒棒糖以上,你可以考慮使用PersistableBundles進行保存,並在重新打開應用程序後檢索狀態。

相關問題