切换标签时,片段有时会重叠

时间:2013-08-28 16:01:38

标签: android android-fragments android-fragmentactivity

我有一个双窗格布局,左边是我的按钮。按下按钮时,右侧会显示相应的片段。有时碎片会重叠,但这是一个间歇性的问题。我无法一直复制它,但它确实发生了

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.imageButtonSettingsManageBooks:
        SettingsManageBooksFragment mbFragment = new SettingsManageBooksFragment();
        getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, mbFragment).addToBackStack(null).commit();
        mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
        this.getSupportFragmentManager().executePendingTransactions();


        break;
    case R.id.imageButtonSettingsPurchaseHistory:
        SettingsPurchaseHistoryFragment phFragment = new SettingsPurchaseHistoryFragment();
        getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, phFragment).commit();
        mImgFragmentTitle.setImageResource(R.drawable.purchase_history);
        this.getSupportFragmentManager().executePendingTransactions();

        break;
    case R.id.imageButtonSettingsAudio:
        SettingsAudioFragment aFragment = new SettingsAudioFragment();
        getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, aFragment).commit();
        mImgFragmentTitle.setImageResource(R.drawable.audio);
        this.getSupportFragmentManager().executePendingTransactions();

        break;
    case R.id.imageButtonSettingsRestore:
        SettingsRestoreFragment rFragment = new SettingsRestoreFragment();
        getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, rFragment).commit();
        mImgFragmentTitle.setImageResource(R.drawable.restore);
        this.getSupportFragmentManager().executePendingTransactions();

        break;
    case R.id.imageButtonSettingsCopyright:
        SettingsCopyrightFragment cFragment = new SettingsCopyrightFragment();
        getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, cFragment).commit();
        mImgFragmentTitle.setImageResource(R.drawable.copyright);
        this.getSupportFragmentManager().executePendingTransactions();

        break;
    case R.id.imageButtonSettingsAbout:
        SettingsAboutFragment abFragment = new SettingsAboutFragment();
        getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, abFragment).commit();
        mImgFragmentTitle.setImageResource(R.drawable.about);
        this.getSupportFragmentManager().executePendingTransactions();

        break;

看起来是因为我用一个可能导致重叠的新片段替换每个片段。有没有办法在提交新片段之前清除所有片段?

1 个答案:

答案 0 :(得分:2)

如上面的StackOverflow所述,这是一个间歇性的问题。我找到了一种解决方法,通过编写自己的代码来删除任何现有的片段,然后再提交新的片段。以下是我的代码:

        fragmentManager = getFragmentManager();
        ft = fragmentManager.beginTransaction();

        mbFragment = new SettingsManageBooksFragment();
        ft.replace(R.id.setting_detail_container2, mbFragment).commit();
        mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.imageButtonSettingsManageBooks:

            if (mPurchaseHistory == true) {
                ft.remove(phFragment);

                Log.d(TAG, "REMOVING PURCHASE HISTORY FRAG");

            } else if (mAudio == true) {
                ft.remove(aFragment);

                Log.d(TAG, "REMOVING AUDIO FRAG");
            } else if (mRestore == true) {
                ft.remove(rFragment);

                Log.d(TAG, "REMOVING RESTORE FRAG");
            } else if (mCopyright == true) {
                ft.remove(cFragment);

                Log.d(TAG, "REMOVING COPYRIGHT FRAG");
            } else if (mAbout == true) {
                ft.remove(abFragment);

                Log.d(TAG, "REMOVING ABOUT FRAG");
            }
            ft = fragmentManager.beginTransaction();
            mbFragment = new SettingsManageBooksFragment();
            ft.replace(R.id.setting_detail_container2, mbFragment).commit();
            mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
            mManageBooks = true;
            mPurchaseHistory = false;
            mAudio = false;
            mRestore = false;
            mCopyright = false;
            mAbout = false;

            break;