prevent相同片段在popBackStack添加多次 [英] Prevent the same Fragment to be added multiple times in popBackStack
问题描述
我的活动是由3嵌套片段。还有就是我的 MainFragment
的默认显示, ProductFragment
可从它被调用,那么 DetailFragment
可以从 ProductFragment
。
My activity is composed of 3 nested Fragments. There is my MainFragment
that is displayed by default, ProductFragment
that can be called from it, then DetailFragment
can be called from ProductFragment
.
我可以回去之间来回我的 ProductFragment
和 DetailFragment
。通过这样做,所述popStackBack方法正在积累相似的片段。然后,如果我点击后退按钮,它会回去通过所有片段尽可能多的时间,我打电话给他们。
I can go back and forth between my ProductFragment
and DetailFragment
. By doing so, the popStackBack method is accumulating similar fragments. Then, if I click on the back button, It will go back through all the Fragments as many time I called them.
什么是被关在后面的堆栈,以避免同一片段的正确方法?
What is the proper way to avoid the same Fragment to be kept in the back stack ?
编辑:
我首先打电话给我的主要片段:
I firstly call my main fragment :
if (savedInstanceState == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.container, new SearchFragment(), "SEARCH_TAG")
.commit();
}
下面是code,从该活动呼吁片段:
Here is the code that calls the fragments from the activity :
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.animator.enter_from_bottom, R.animator.exit_to_top, R.animator.enter_from_bottom, R.animator.exit_to_top);
ft.replace(R.id.container, new FactFragment(), "FACT_TAG");
ft.addToBackStack("FACT_TAG");
ft.commit();
然后,在后面点击:
Then, on back click :
@Override
public void onBackPressed() {
getFragmentManager().popBackStack();
}
我试图让我的当前片段的标签,并执行有关它的一些具体的code,但它不能很好地工作。我也试图 addToBackStack()
仅在当前片段是不是已经加入到backStack但它搞砸了我的片段视图。
I tried to get the tag of my current fragment and execute some specific code related to it but it doesn't work well. I also tried to addToBackStack()
only when current Fragment wasn't already added to the backStack but it messed up my fragment view.
推荐答案
下面是我的解决方案。也许很脏,但它的工作原理。我实现它返回一个点击之前所显示的片段的标签的方法上背面按钮:
Here is my solution. Maybe dirty but it works. I implemented a method that returns the tag of the fragment that is displayed before clicking the on back button :
public String getActiveFragment() {
if (getFragmentManager().getBackStackEntryCount() == 0) {
return null;
}
String tag = getFragmentManager()
.getBackStackEntryAt(getFragmentManager()
.getBackStackEntryCount() - 1)
.getName();
return tag;
}
然后,在我的 onBack pressed()
方法:
// Get current Fragment tag
String currentFrag = getActiveFragment();
if(currentFrag.equals("PRODUCT_TAG")) {
// New transaction to first Fragment
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.animator.enter_from_right, R.animator.exit_to_left, R.animator.enter_from_right, R.animator.exit_to_left);
ft.replace(R.id.container, new SearchFragment(), "MAIN_TAG");
ft.commit();
} else {
// Go to Fragment-1
getFragmentManager().popBackStack();
}
这篇关于prevent相同片段在popBackStack添加多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!