fragment保存數據後返回到指定fragment

 移出後添加的fragment,展示指定的fragment:

添加fragment時候  需要fragment設置Tag,利用Tag找到指定fragment

public void rollBackAppointFragment(String fragmentName) {
        int fragmentCount = getSupportFragmentManager().getBackStackEntryCount();

        if ("".equals(fragmentName) || fragmentName == null) {
            while (fragmentCount > 1) {
//            getSupportFragmentManager().popBackStack();
                getSupportFragmentManager().popBackStackImmediate();
                fragmentCount--;
            }
        } else {
            for (int i = fragmentCount - 1; i > -1; i--) {
                FragmentManager.BackStackEntry entry = getSupportFragmentManager().getBackStackEntryAt(i);
                
                getSupportFragmentManager().popBackStackImmediate();
                if (entry.getName().equals(fragmentName)) {
                    break;
                }
            }
        }
      
    }

 

封裝的兩個添加fragment的方法:

在addFragment和switchContent方法中,對隱藏的fragment添加Tag(fragment.getClass().getSimpleName()),方便後面跳轉到指定fragment使用(也可以用fragment  Id)

//添加fragment,移除了當前的fragment,添加新fragment。點擊返回鍵fragment會重建
public void addFragment(BaseFragmentTwo fragment) {
    if (fragment != null) {
        getSupportFragmentManager().beginTransaction()
                //transaction.replace(R.id.fragment_container,fragment,tag);
                //fragment.getClass().getSimpleName() 就是fragment的Tag
                .replace(R.id.frameLayout, fragment, fragment.getClass().getSimpleName())
                .addToBackStack(fragment.getClass().getSimpleName())
                .commitAllowingStateLoss();
    }
}

//添加fragment,隱藏了當前fragment,點擊返回時當前的視圖和數據還依然存在
public void switchContent(Fragment from, Fragment to) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(
            android.R.anim.fade_in, R.anim.abc_fade_out);
    if (!to.isAdded()) {    // 先判斷是否被add過
        transaction.hide(from);
        transaction.add(R.id.frameLayout, to);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.addToBackStack(from.getClass().getSimpleName());
        transaction.commit(); // 隱藏當前的fragment,add下一個到Activity中
    } else {
        transaction.hide(from).show(to).commit(); // 隱藏當前的fragment,顯示下一個
    }
}

 

 下面兩個不執行的方法(沒搞清楚怎麼解決,看源碼是需要mStateSaved這個參數的值設置爲false,不然會報異常,下面代碼就不執行了,清楚的高手希望留言交流,項目緊張,後面抽空解決會回來更新)

popBackStack(String tag,int flags)

tag可以爲null或者相對應的tag,flags只有0和1(POP_BACK_STACK_INCLUSIVE)兩種情況

如果tag爲null,flags爲0時,彈出回退棧中最上層的那個fragment。

如果tag爲null ,flags爲1時,彈出回退棧中所有fragment。

如果tag不爲null,那就會找到這個tag所對應的fragment,flags爲0時,彈出該

fragment以上的Fragment,如果是1,彈出該fragment(包括該fragment)以

上的fragment。

getActivity().getSupportFragmentManager().popBackStackImmediate(CreateGroupNameFragment.class.getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);

即彈出Tag爲CreateGroupNameFragment之上的所有(包括自身)的Fragment。

popBackStackImmediate(name,flag);

 


 

第二個參數:只能是 0 或者 1(POP_BACK_STACK_INCLUSIVE);

第一個參數爲null時,第二個參數爲0時:

會彈出回退棧中最上層的那一個fragment。

因爲經測試,回退棧中的fragment個數減少了一個。

第二個參數爲1時:會彈出所有回退棧中的fragment。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章