I excute switchCompat.setChecked(true)
in the onViewCreated(...)
of Fragment.It works well first time but not working after back from the BackStack.
The log show that switchCompat.isChecked()
is true,but the UI state of SwitchCompat is off.It is so strage that the parms named isChecked of onCheckedChanged in OnCheckedChangeListener is also false.
Could anyone tell me why ?
The Fragment
@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());
}
Switch to next Fragemnt
private void switchFragment(Fragment fragment,String tag){
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.replace(android.R.id.content, fragment, tag);
fragmentTransaction.commit();
}
Back button to back from BackStack
@Override
public void onBackPressed() {
//回退栈中有Fragment
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
} else {
getSupportFragmentManager().popBackStack();
}
}
**In the Second Frahment **
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mAlarm = getArguments().getParcelable(PARAM_ALARM);
}
mAlarm.setSnooze(true);
}
Call initialSnooze(); in onResume()