Android 为什么 switchCompat.setChecked(true) 从 BackStack 返回后不工作

Android Why switchCompat.setChecked(true) not working after back from BackStack

我在 FragmentonViewCreated(...) 中执行 switchCompat.setChecked(true)。第一次运行良好,但从 BackStack 返回后不工作。

日志显示switchCompat.isChecked()true,但是SwitchCompat的UI状态是off.It 非常奇怪 OnCheckedChangeListeneronCheckedChanged 的参数 isChecked 也是

谁能告诉我为什么?

片段

  @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ......other code......

    initialSnooze();

}


private void initialSnooze() {
    mSwitchSnooze.setChecked(mAlarm.isSnooze());
    Log.d("TAG", "initialSnooze: "+mAlarm.toString()+" "+mSwitchSnooze.isChecked());
}

切换到下一个片段

 private void switchFragment(Fragment fragment,String tag){
        FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(tag);        
        fragmentTransaction.replace(android.R.id.content, fragment, tag);
        fragmentTransaction.commit();
    }

后退按钮从 BackStack 返回

    @Override
public void onBackPressed() {
    //回退栈中有Fragment
    int count = getSupportFragmentManager().getBackStackEntryCount();
    if (count == 0) {
        super.onBackPressed();
    } else {
        getSupportFragmentManager().popBackStack();
    }
}

**在第二个碎片中**

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mAlarm = getArguments().getParcelable(PARAM_ALARM);
    }
    mAlarm.setSnooze(true);
}

调用initialSnooze();在 onResume()