尝试避免将相同的片段两次添加到backStack,popBackStackImmediate始终返回false [英] Trying avoid add same fragment twice to backStack, popBackStackImmediate always return false
问题描述
我正在尝试避免使用此方法将相同的片段添加到backStack:
public static void replaceFragment(FragmentManager fragmentManager, Fragment fragment, Boolean addToBackStack) {
String backStateName = fragment.getClass().getName();
boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
if (addToBackStack && !fragmentPopped && fragmentManager.findFragmentByTag(backStateName) == null) {
fragmentManager
.beginTransaction()
// .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.fade_in, android.R.anim.fade_out)
.replace(R.id.container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(backStateName) // was 'backStateName'
.commit();
} else {
if (!addToBackStack)
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager
.beginTransaction()
// .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.fade_in, android.R.anim.fade_out)
.replace(R.id.container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.disallowAddToBackStack()
.commit();
}
}
导航抽屉包含:具有profilePhoto
的ImageView,可打开ProfileMainFragment
,并且具有几个类别,可打开MainFragmentCategory
.从片段navigationDrawerMenu
中选择片段时,Boolean addToBackStack
是false
;在片段中(从MainFragmentCategory
移到DetialFragmentCategory
中)选择true
时,或单击navigationDrawer
中的profilePhoto
.
fragmentPopped
始终是false
,为什么会这样呢?即使我单击profilePhoto
并再次单击navigationDrawer
中的profilePhoto
.应该避免第二次(第三次,第四次)将其添加到backStack,但是没有.
有什么主意我该怎么做?
您可以在替换片段之前添加以下代码.
// Replace fragmentCotainer with your container id
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragmentCotainer);
// Return if the class are the same
if(currentFragment.getClass().equals(fragment.getClass())) return;
I'm trying avoid adding the same fragment to backStack with this method:
public static void replaceFragment(FragmentManager fragmentManager, Fragment fragment, Boolean addToBackStack) {
String backStateName = fragment.getClass().getName();
boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
if (addToBackStack && !fragmentPopped && fragmentManager.findFragmentByTag(backStateName) == null) {
fragmentManager
.beginTransaction()
// .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.fade_in, android.R.anim.fade_out)
.replace(R.id.container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(backStateName) // was 'backStateName'
.commit();
} else {
if (!addToBackStack)
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager
.beginTransaction()
// .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.fade_in, android.R.anim.fade_out)
.replace(R.id.container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.disallowAddToBackStack()
.commit();
}
}
Navigation Drawer contains: ImageView with profilePhoto
which opens ProfileMainFragment
and few categories which opens MainFragmentCategory
. Boolean addToBackStack
is false
when fragment is choosen from navigationDrawerMenu
and true
when is choosen within fragment (move from MainFragmentCategory
to DetialFragmentCategory
) or click profilePhoto
in navigationDrawer
.
fragmentPopped
is always false
, why is that so? Even if I click profilePhoto
and again profilePhoto
in navigationDrawer
. It should avoid to add it to backStack for a second (and third, and fourth...) time, but it didn't.
Any idea how can I make it right?
You can add the following code before you replace your fragment.
// Replace fragmentCotainer with your container id
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragmentCotainer);
// Return if the class are the same
if(currentFragment.getClass().equals(fragment.getClass())) return;
这篇关于尝试避免将相同的片段两次添加到backStack,popBackStackImmediate始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!