简体   繁体   中英

Fragment replace not work until performe the second time

Hey ,everybody,what a nice day! I am making an App which have a RadioGroup(Customed) in the bottom of screen.And when first enter into the App,it will show you the welcome fragment like below:(sorry,I can't upload pictureT^T,though I know I can post a picture link here⊙﹏⊙)

------------------------------------ | | | | | | | | | | | | | | | welcome | | | | | | | | | | | | | | | | | | | |---------------------------------- | | | |⊙tab0 ⊙tab1 ⊙tab2 | -------------------------------------

And the RadioButton is not checked.

When I touch the tab0,it will change to another fragment,like below:

------------------------------------ |<back | | | | | | | | | | | | | | tab0 | | content | | | | | | | | | | | | | | | | | |---------------------------------- | | | |*⊙tab0* ⊙tab1 ⊙tab2 | -------------------------------------

It seems work properly.But when hit the back button,it not worked ,when I hit again,it worked.It's so strange. Here is my WelcomeFragment codes:

public class WelcomeFragment extends Fragment {

private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.welcome_fragment_layout, null);
    MainActivity.radioGroup.setBackgroundResource(R.drawable.tabbar_bg);
    RadioButton button;
    // Reset the RadioGroup state to not checked。
    for(int i = 0;i<3;i++){
        button = (RadioButton)MainActivity.radioGroup.getChildAt(i);
        //button.setChecked(false);//if I comment this,it work finely!!,but I don't know why                
    }
    return view;
}

It's radiobuttons' event in Activity:

    private void setupRadioGroup() {
    resetRadioButtonID();
    radioButton = (RadioButton) radioGroup.getChildAt(0);
    radioButton.setChecked(false);
    radioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if (checkedId != 1) {
                        radioButton.setChecked(false);
                    }
                    FragmentTransaction transaction = fragmentManager
                            .beginTransaction();
                    Fragment fragment = FragmentFactory
                            .getInstanceByIndex(checkedId);
                    transaction.replace(R.id.fl_content, fragment);
                    transaction.commitAllowingStateLoss();


                    switch (checkedId) {
                    case 1:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar1);
                        break;
                    case 2:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar2);
                        break;
                    case 3:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar3);
                        break;
                    default:
                        break;
                    }
                }
            });
}

I will appreciateit it very much,BTW ,don't mind my poor English .^-^

You need to wrap everything inside the onCheckedChanged inside an if condition. Check for the current status of the radio button first.

radioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(!radioButton.checked) return;  // add this
                    if (checkedId != 1) {
                        radioButton.setChecked(false);
                    }
                    FragmentTransaction transaction = fragmentManager
                            .beginTransaction();
                    Fragment fragment = FragmentFactory
                            .getInstanceByIndex(checkedId);
                    transaction.replace(R.id.fl_content, fragment);
                    transaction.commitAllowingStateLoss();


                    switch (checkedId) {
                    case 1:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar1);
                        break;
                    case 2:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar2);
                        break;
                    case 3:
                        radioGroup.setBackgroundResource(R.drawable.menu_bar3);
                        break;
                    default:
                        break;
                    }
                }
            });

Thank Premsuraj. There is nothing about the Fragment.It's caused by RadioGroup OnCheckedChange event. Let's look the source of RadioGroup:

/**
 * <p>Changes the checked state of this button.</p>
 *
 * @param checked true to check the button, false to uncheck it
 */
public void setChecked(boolean checked) {
    if (mChecked != checked) {
        mChecked = checked;
        refreshDrawableState();
        notifyViewAccessibilityStateChangedIfNeeded(
                AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);

        // Avoid infinite recursions if setChecked() is called from a listener
        if (mBroadcasting) {
            return;
        }

        mBroadcasting = true;
        if (mOnCheckedChangeListener != null) { 
            mOnCheckedChangeListener.onCheckedChanged(this, mChecked);//This is the root cause
        }
        if (mOnCheckedChangeWidgetListener != null) {
            mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
        }

        mBroadcasting = false;            
    }
}

Because I called this method in Fragment not in listener,so ,when I call setChecked(boolean checked),the onCheckedChanged() also invoked.It's the root of problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM