6

我有两个窗格布局,左侧有按钮。当按下按钮时,其对应的片段显示在右侧。有时片段重叠,但这是一个间歇性问题。我不能一直复制它,但它确实发生了

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;

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

4

1 回答 1

2

正如上面 StackOverflowed 所提到的,这是一个间歇性问题。我找到了解决此问题的方法,方法是编写自己的代码以在提交新片段之前删除任何现有片段。下面是我的代码:

        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;
于 2013-09-12T09:20:32.870 回答