Avoid recreating same view when perform tab switching

后端 未结 6 598
一生所求
一生所求 2021-01-30 17:43

Current, I have 2 Fragments, which is switch-able through ActionBar\'s tab.

    getSupportActionBar().setNavigationMode(ActionBar.NAVIG         


        
6条回答
  •  礼貌的吻别
    2021-01-30 18:05

    I faced the same issue, but what I did was, before attaching or detaching the fragement inside the callbacks of ActionBar.TabListener, call

    fragmentManager.executePendingTransactions();
    

    this solves the issue for me

    @Override
    public void onTabelected(Tab tab, FragmentTransaction ft, FragmentManager fm) {
        fm.executePendingTransactions(); // **execute the pending transactions before adding another fragment.
        if (mFragment == null) {
            mFragment = Fragment.instantiate(mContext, mFragmentName);
            ft.replace(android.R.id.tabcontent, mFragment, mTag);
        } else {
            ft.attach(mFragment);
        }
    }
    

提交回复
热议问题