2011-10-22 123 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();