2014-01-26 94 views
0

我的ToggleButton出現問題。Android ToggleButton暫停/恢復問題

我第一次點擊它時,它會靜音媒體聲音。第二次,它取消了它們。爲了保存ToggleButton的狀態,我使用了onPause/onResume方法。但問題是,如果我啓動活動,然後我按切換按鈕,我暫停它,當我最終恢復它,切換按鈕不再工作,應用程序保持靜音。 (即使狀態保存)

我的活動部分:

public void onToggleClicked(View view) { 
    // Is the toggle on? 
    boolean on = ((ToggleButton) view).isChecked(); 


    if (on) { 
     AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE); 
      aManager.setStreamMute(AudioManager.STREAM_MUSIC, true); 

    } else { 
     AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE); 
     aManager.setStreamMute(AudioManager.STREAM_MUSIC, false); 


    } 

} 


@Override 
public void onPause() { 
    super.onPause(); 
    bundle.putBoolean("ToggleButtonState", ToggleButton.isChecked()); 

} 

@Override 
public void onResume() { 
    super.onResume(); 
    ToggleButton.setChecked(bundle.getBoolean("ToggleButtonState",false)); 
} 

我知道,我也許應該什麼地方使用另一個暫停/恢復的代碼,你能幫忙嗎?謝謝:)

這裏的日誌當我打開應用程序,打開按鈕的活動,按一下按鈕,暫停活動,恢復,請再次點擊該按鈕,暫停再次活動:

01-01 04:19:55.004: D/OpenGLRenderer(10766): Enabling debug mode 0 

01-01 04:19:55.113: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9273361 

01-01 04:20:00.902: I/ActivityManager(10766): Timeline: Activity_launch_request id:com.example.appquiz time:9279155 

01-01 04:20:01.121: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9279369 

01-01 04:20:05.840: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9284092 

01-01 04:20:09.988: I/ActivityManager(10766): Timeline: Activity_launch_request id:com.example.appquiz time:9288234 

01-01 04:20:10.168: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9288414 

01-01 04:20:20.566: I/ActivityManager(10766): Timeline: Activity_idle id: [email protected] time:9298814 
+0

您是否嘗試過在您的onresume和暫停方法中輸出日誌以查看該卡在靜音時的值,您擁有的值編輯,仍然是你退出的價值? –

+0

你如何獲得捆綁? –

+0

Bundle:private static Bundle bundle = new Bundle(); –

回答

2

時你的活動被破壞了,你不再保存狀態,當你回到它時,oncreate再次運行,你的包是空的,所以「ToggleButton」鍵的選項是空的,因此返回false,並且您的切換按鈕保持靜音

+0

我添加了像這樣的savedInstanceState方法:http://www.beetxt.com/qUM/但它仍然不起作用:( –

+1

將它放在共享首選項中,保存實例狀態是在活動 –

+1

onPause SharedPreferences之間移動。編輯器編輯器= getSharedPreferences(「my_settings」,0).edit(); editor.putBoolean(「ToggleButton」,true/**無論它應該在這裏* /)commit();然後 onResume SharedPreferences prefs = getSharedPreferences (「my_settings」,0); boolean ToggleButtonState = prefs.getBoolean(「ToggleButton」,true); –