popBackStack导致一次又一次调用片段的oncreateView [英] popBackStack causes calling oncreateView of fragment again and again

查看:179
本文介绍了popBackStack导致一次又一次调用片段的oncreateView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个片段A,B,C.我编写了一段代码来替换它们并维护后退堆栈:

I have 3 fragment A, B,C.I wrote piece of code for replacing them and maintaining backstack:

 public void addFragment(Fragment fragmentToAdd, String fragmentTag) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        Fragment activeFragment = getActiveFragment();
        FragmentTransaction fragmentTransaction = supportFragmentManager
                .beginTransaction();
        if (null != activeFragment) {
            fragmentTransaction.hide(activeFragment);
        }
        fragmentTransaction.replace(R.id.layout_child_activity, fragmentToAdd,
                fragmentTag);

       if (supportFragmentManager.getBackStackEntryCount() > 1) {
            supportFragmentManager.popBackStack();
        }
        fragmentTransaction.addToBackStack(fragmentTag);
        fragmentTransaction.commit();
    }

这是这段代码

if (supportFragmentManager.getBackStackEntryCount() > 1) {
    supportFragmentManager.popBackStack();
}

如果堆栈长度大于1,我将最新的片段用于弹出.现在,由于这个原因,当长度大于1时,它会一次又一次地调用onCreate视图. 像:

I using for pop the latest fragment if stack length is more than 1. Now due to this when length is going greater than 1 than it is calling onCreate view again and again. Like :

  1. 打开A.
  2. 打开B.
  3. 打开C. (如果打开C,则调用A的onCreateView.)

为什么我会出现这种行为?当我删除该斜体代码时,它没有发生.

Why I am getting such kind of behavior ? When I am removing that italic code than it is not happening.

推荐答案

比行为正常的是,如文档所说,来自后台堆栈事务. Backstack永远不会保存Fragments,而只会保存事务

Than behavior is normal, coming from the backstack transaction, as the docs say. The backstack never saves Fragments, it just saves the transaction

http://developer.android.com/intl/es /guide/components/fragments.html

我不确定(如果不确定)这是最好的方法,但是 当我想清除所有交易时,我会这样做

What I do, I am not sure if, is it the best way but when I want to clear all the transactions I do this

1)在您的活动中检查后备堆栈中是否有任何交易, 并在片段中添加一个标志,如果是A

1) INSIDE YOUR ACTIVITY check if is there any transactions in the back stack, and add a flag inside your fragment, in your case is A

       int backStackCount = getSupportFragmentManager().getBackStackEntryCount();

       if(backStackCount > 0) {
           Transactions.MUST_DETACH_FROM_BACKSTACK = true;
           getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
       }

2)在片段A内,获取标志并删除片段onCreateView并返回null,像这样

2) Inside your fragment A, get the flag and Remove the fragment onCreateView and return null like this

public class Transactions extends android.support.v4.app.Fragment{

public static boolean MUST_DETACH_FROM_BACKSTACK = false;

public Transactions() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.i("FRAGMENT", "onCreateView "+MUST_DETACH_FROM_BACKSTACK);
    // Inflate the layout for this fragment
    if (MUST_DETACH_FROM_BACKSTACK) {
        MUST_DETACH_FROM_BACKSTACK = false;
        getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
        return null;
    }
    return inflater.inflate(R.layout.fragment_transactions, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    Log.i("FRAGMENT", "onViewCreated");
    if(view != null){

        Log.i("FRAGMENT", "ThreadStarted");
        startThread(view);
    }
}

请小心,我在

OnCreateView()

即使具有getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();

even with getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();

因此,如果您有任何conde onResume方法,则应正确处理它

So if you have any conde onResume method you should handle it properly

这篇关于popBackStack导致一次又一次调用片段的oncreateView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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