Android 片段问题(多个实例)
我有一个讨厌的问题,我不知道怎么解决。问题很简单: 我有一个Android 片段问题(多个实例),android,fragment,multiple-instances,Android,Fragment,Multiple Instances,我有一个讨厌的问题,我不知道怎么解决。问题很简单: 我有一个FragmentA,点击一个按钮就可以按下FragmentB(aListView)。 在FragmentB中,我可以用onimclick来推动FragmentA。你看,深度是无限的 问题是当我第二次按FragmentB时,然后返回到FragmentB的第一个实例(2次),我在列表视图中有第一个和第二个实例中的项目。如果我要创建10个实例,那么我将在第一个实例中拥有所有10个实例的项目 谁能解释一下这个问题,并给我一个解决方案吗 编辑(代
FragmentA
,点击一个按钮就可以按下FragmentB
(aListView
)。
在FragmentB
中,我可以用onimclick
来推动FragmentA
。你看,深度是无限的
问题是当我第二次按FragmentB
时,然后返回到FragmentB
的第一个实例(2次),我在列表视图中有第一个和第二个实例中的项目。如果我要创建10个实例,那么我将在第一个实例中拥有所有10个实例的项目
谁能解释一下这个问题,并给我一个解决方案吗
编辑(代码段):
您可以使用FragmentTransaction
transaction.replace(R.id.fragment\u容器,newFragment)代码>
此问题与多个实例无关。是一个监听器
提供了问题,我的错误。谢谢你的努力。你有多少碎片…??我有这两个碎片,碎片A和碎片B。但它们可以彼此启动(圆形)。所以你看,深度是无限的。-如果每次都使用新实例。但您只能在这些片段的两个实例之间切换,从而避免问题。我在ListView中有第一个和第二个实例中的项目-您是否有机会在该片段中有静态
数据?我正在用ft.replace(R.id.content_frame,fragment,“current”)推送frag;然后是ft.addToBackStack(null);FollowersFragment=新FollowersFragment()@肉体仍然有问题..?是的,我仍然有探测器。你是动态生成碎片吗。。??
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();
}
});
}
}
}