Get fragment from backstack for second time

冷暖自知 提交于 2019-12-25 08:28:02

问题


i'm currently working on browser app for an android. I'm managing new tabs with fragments. So everything is working fine when user open new tab B, and then go back again to tab A. The problem appear when from A he try to go back again to B. With other words its not working when you tries to pop back same fragment for the second time.

This is how I'm adding new tabs- fragments

    android.app.Fragment f1 = new FragmentHolder();
    allFragments.add(f1);
    getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, f1).addToBackStack(fragmentTags[allFragments.size()-1]).commit();

and here how I'm popping back the fragment

boolean chkFlag = getFragmentManager().popBackStackImmediate(currentTag.getFragmentTAG(),0);

I appreciate your help in advance.


回答1:


Use below function in your Activity,

private void loadFragmentAnimated(Fragment fragment, Bundle args, int containerId, String title)
    {
        fragment.setArguments(args);
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(containerId, fragment);
        fragmentTransaction.commitAllowingStateLoss();
    }

Then Added Fragment by using ,

loadFragmentAnimated(c, null, R.id.container_name, "title");

And remove fragment by using,

getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.id.container_name)).commit();


来源:https://stackoverflow.com/questions/42623912/get-fragment-from-backstack-for-second-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!