0

我有这个烦人的问题,我不知道如何解决。问题很简单:

我有FragmentA哪个按钮点击FragmentB(a ListView)。在FragmentB我可以FragmentAOnItemClick. 所以你看,深度是无限的。

问题是当我FragmentB第二次推送,然后返回(2 次)到第一个实例时,FragmentB我在ListView. 如果我要制作 10 个实例,那么我将在第一个实例中拥有所有 10 个实例的项目。

谁能解释这个问题并请给我一个解决方案?

编辑(代码片段):

FollowersFragment frag = new FollowersFragment();
Bundle bundle = new Bundle();
bundle.putString(Constants.USER_ID, userId);
frag.setArguments(bundle);
((MainActivity) getActivity()).pushFragment(frag);

public void pushFragment(TrigdFragment fragment) {
    pushFragment(fragment, new AnimationObject());
}

public void pushFragment(TrigdFragment fragment, AnimationObject animate) {
    switchContent(fragment, animate, false);
}

public void switchContent(TrigdFragment fragment, AnimationObject anim,
        boolean clearBackStack) {
    ActionBarHelper mActionBarHelper = ActionBarHelper.getInstance();
    supportInvalidateOptionsMenu();
    FragmentManager mgr = getSupportFragmentManager();

    if (clearBackStack) {
        mActionBarHelper.setDisplayHomeAsDrawerEnabled(true);
        mgr.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    } else {
        mActionBarHelper.setDisplayHomeAsUpEnabled(true);
    }

    fragment.setupActionBar(getResources());

    FragmentTransaction ft = mgr.beginTransaction();

    boolean doingAnimation = false;
    if (Util.hasIcecreamSandwich()) {
        doingAnimation = anim != null;

        if (doingAnimation) {
            ft.setCustomAnimations(anim.enterResource, anim.exitResource,
                    anim.popEnterResource, anim.popExitResource);
        }
    }
    ft.replace(R.id.content_frame, fragment, "current");

    if (!clearBackStack) {
        ft.addToBackStack(null);
    }

    ft.commitAllowingStateLoss();

    if (Util.hasIcecreamSandwich()) {
        if (doingAnimation) {
            // This can't be done immediately because the transaction may
            // not
            // yet be committed. Commits are are posted to the main
            // thread's message loop.
            mHandler.post(new Runnable() {
                @SuppressLint("NewApi")
                @Override
                public void run() {
                    invalidateOptionsMenu();
                }
            });
        }
    }
}
4

2 回答 2

0

replace您可以使用以下功能,而不是创建片段的新实例FragmentTransaction

transaction.replace(R.id.fragment_container, newFragment);

于 2013-08-07T12:27:08.010 回答
0

此问题与多个实例无关。这是一个Listener问题,我的错误。不过感谢您的努力。

于 2013-08-13T07:37:47.967 回答