尝试避免将相同的片段两次添加到backStack,popBackStackImmediate始终返回false [英] Trying avoid add same fragment twice to backStack, popBackStackImmediate always return false

查看:229
本文介绍了尝试避免将相同的片段两次添加到backStack,popBackStackImmediate始终返回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 addToBackStackfalse;在片段中(从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆