Android開發相關:(02)使用首選項保存活動的狀態

[java] final String INITIALIZED = "initialized"; 
SharedPreferences myPrefs = getPreferences(MODE_PRIVATE); 
 
boolean hasPreferences = myPrefs.getBoolean(INITIALIZED, false); 
if (hasPreferences) { 
    Log.v("Preferences", "We've been called brefore"); 
    // Read other values as desired from preferences file…  
    someString = myPrefs.getString("someString", ""); 
} else { 
    Log.v("Preferences", "First time ever being called"); 
    // Set up initial values for what will end up   
    // in the preferences file  
    someString = "Some default value"; 

// Later when ready to write out values  
Editor editor = myPrefs.edit(); 
editor.putBoolean(INITIALIZED, true); 
editor.putString("someString", someString); 
// Write other values as desired  
editor.commit(); 

摘自 horsttnann的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。