Android Why switchCompat.setChecked(true) not working after back from BackStack
<p>I excute <code>switchCompat.setChecked(true)</code> in the <code>onViewCreated(...)</code> of <strong>Fragment</strong>.It works well first time but not working after back from the BackStack.<br><br>The log show that <code>switchCompat.isChecked()</code> is <strong>true</strong>,but the UI state of <strong>SwitchCompat</strong> is off.It is so strage that the parms named <strong>isChecked</strong> of <strong>onCheckedChanged</strong> in <strong>OnCheckedChangeListener</strong> is also <strong>false</strong>.
<br><br>
Could anyone tell me why ?</p>

<p><strong>The Fragment</strong></p>

<pre><code>  @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());
}
</code></pre>

<p><strong>Switch to next Fragemnt</strong></p>

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

<p><strong>Back button to back from BackStack</strong></p>

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

<p>**In the Second Frahment **</p>

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