2012-05-20 37 views
0

我要強制使用,以填補設置頁面在兩種情況下的android共享偏好問題

1)當用戶第一次啓動應用 2)當數據庫版本變化我想,設置頁面應過程使用

之前,首先填寫

在我的環境類別i設定共享偏好「假」,然後我檢查它在下面的代碼

類設置//主類 字符串標誌= sharedPreferences.getString(「CreatedFlag」,「」);

 if(flag.equals("true")) 
     { 
      // Move to second activity 
      Intent i =new Intent(); 
      i.setClass(someclass.this,otherPage.class); 
      startActivity(i); 
      finish(); 
     } 
    else 
    { // Stay on Settings page } 

問題:它在用戶第一次啓動應用程序,它顯示設置頁面,並填寫THT頁, 但是,當用戶運行的應用程序第二次它再次顯示設置頁面,怎麼共享偏好依然有真正的價值運行正常, THN再度用戶運行的應用第三次共享偏好有更新值是假的,並顯示其他網頁

我想是謂設置頁面只顯示一次,如果沒有設置定義,其他明智它轉到主頁

需要幫助,

回答

0

我不在這裏,你實際上保存的偏好,我認爲它在其他頁面的活動?

如果沒有這個設置頁面上添加的第一個活動

SharedPreferences prefs = this.getsharedPreferences("myApp",0); 
//check for the Created value in the shared preferences 
String createdFlag = prefs.getString("Created","false"); // set default value == false 
if(createdFlag == "false){ 
    goToSettingsPage(); // to the settings page we go... 
} 

然後:

SharedPreferences prefs = this.getSharedPreferences("myApp",0); 
SharedPreferences.Editor ed = prefs.edit(); // create the editor 
ed.putString("Created","true"); // put the value to the prefs 
ed.commit(); // commit the changes 

第一次運行之後,共享的首選項文件將被保留,然後不應該轉到第二次運行設置頁面。