popBackStack导致一次又一次地调用fragment的fragment

我有3个片段A,B,CI写了一段代码来替换它们并保留后台堆栈:

 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视图。 喜欢 :

  • 打开A.
  • 打开B.
  • 打开C. (在打开的情况下,调用A的onCreateView)。
  • 为什么我会得到这样的行为? 当我删除斜体代码时,它不会发生。


    正如文档所述,行为是正常的,来自于后台交易。 后台,从不保存碎片,它只是保存交易

    在这里输入图像描述

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

    我做什么,我不确定是否是最好的方式,但是当我想清除所有交易时,我会这样做

    1)在你的活动中检查是否有任何交易在后退堆栈中,并在你的片段内添加一个标志,在你的情况下是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

    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);
        }
    }
    

    但请小心,我得到onResume()后调用

    OnCreateView()
    

    即使使用getActivity()。getSupportFragmentManager()。beginTransaction()。remove(this).commit();

    所以如果你有任何conde onResume方法,你应该适当地处理它

    链接地址: http://www.djcxy.com/p/78041.html

    上一篇: popBackStack causes calling oncreateView of fragment again and again

    下一篇: Apache Nutch: Get outlink URL's text context