Fragment Transaction without commit

2019-08-04 09:56发布

问题:

I have seen the code below and unable to figure it out.

if (mGoalProgressFragment != null) {
  mCallerFramgent.getActivity().getSupportFragmentManager().beginTransaction().show(mGoalProgressFragment);
}
mCallerFramgent.getActivity().getSupportFragmentManager().beginTransaction().commit();

i am not able to understand the part when the if condition fails and commit() is called. and when if condition is true commit is not used for show transaction.

Can anyone help me understand this. because lint shows missing commit for the transaction inside the if selection.

回答1:

Use

...beginTransaction().show(mGoalProgressFragment).commit();

call commit method when mGoalProgressFragment is not null and also remove other statement in which you are calling commit method which is not useful.show useful message in else block when mGoalProgressFragment is null.