2011-10-22 124 views
0

我的应用程序允许用户为每次启动应用程序时选择默认屏幕,并使用SharedPreferences来执行此操作。应用程序会在安装后第一次启动时提示他们选择一个屏幕,并且该部分有效。但是,在允许用户更改默认屏幕的应用程序中,我使用相同的代码,并且它从不存储更改。我需要改变什么才能正确保存?允许用户更改默认屏幕

  AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Choose a Default Screen"); 
     builder.setItems(R.array.openChoices, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 
       SharedPreferences settings = getPreferences(0); 
       SharedPreferences.Editor editor = settings.edit(); 
       editor.putInt("start", item); 
       editor.commit(); 
       //Mech = 0, E&M = 1 
       int choice = getPreferences(0).getInt("start", 3); 
       if(choice == 0){ 
        Toast.makeText(setscreen.this, "Mechanics is now the default screen", Toast.LENGTH_SHORT).show(); 
        Intent myIntent = new Intent(setscreen.this, physics.class); 
        startActivity(myIntent);    
       } 
       else if(choice == 1){ 
        Toast.makeText(setscreen.this, "E&M is now the default screen", Toast.LENGTH_SHORT).show(); 
        Intent myIntent = new Intent(setscreen.this, physicsem.class); 
        startActivity(myIntent); 
       } 
      } 
     }); 
+0

你有没有试过调用editor.apply()? – Jack

+0

不应该使用editor.commit()做同样的事情吗? – tedbrooks2

+0

是的,文档说他们也是这样做的,我只是说它值得一试:)。 – Jack

回答

0
使用像这样得到一个关于你共享偏好

尝试对象:

myPrefs = PreferenceManager.getDefaultSharedPreferences(ScreenSaverActivity.this); 

我不肯定知道你已经完成了它的方式是行不通的,但我从来没有见过这样做只是通过一个0.

+0

好吧,我会给这个镜头,看看它是如何工作的。谢谢 – tedbrooks2

0

你确定你每次都打电话editor.commit();