2012-05-20 35 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 

第一次运行之后,共享的首选项文件将被保留,然后不应该转到第二次运行设置页面。