Prevent the same Fragment to be added multiple times in popBackStack

百般思念 提交于 2019-12-11 02:58:08

问题


My activity is composed of 3 nested Fragments. There is my MainFragment that is displayed by default, ProductFragment that can be called from it, then DetailFragment can be called from ProductFragment.

I can go back and forth between my ProductFragment and DetailFragment. By doing so, the popStackBack method is accumulating similar fragments. Then, if I click on the back button, It will go back through all the Fragments as many time I called them.

What is the proper way to avoid the same Fragment to be kept in the back stack ?

EDIT :

I firstly call my main fragment :

    if (savedInstanceState == null) {
        getFragmentManager()
            .beginTransaction()
            .add(R.id.container, new SearchFragment(), "SEARCH_TAG")
            .commit();
    }

Here is the code that calls the fragments from the activity :

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.animator.enter_from_bottom, R.animator.exit_to_top, R.animator.enter_from_bottom, R.animator.exit_to_top);
    ft.replace(R.id.container, new FactFragment(), "FACT_TAG");
    ft.addToBackStack("FACT_TAG");
    ft.commit();

Then, on back click :

@Override
public void onBackPressed() {
    getFragmentManager().popBackStack();
}

I tried to get the tag of my current fragment and execute some specific code related to it but it doesn't work well. I also tried to addToBackStack() only when current Fragment wasn't already added to the backStack but it messed up my fragment view.


回答1:


Use fragment's method isAdded() to evaluate the insertion. For example:

if(!frag.isAdded()){
   //do fragment transaction and add frag
}



回答2:


Here is my solution. Maybe dirty but it works. I implemented a method that returns the tag of the fragment that is displayed before clicking the on back button :

public String getActiveFragment() {

    if (getFragmentManager().getBackStackEntryCount() == 0) {
        return null;
    }

    String tag = getFragmentManager()
        .getBackStackEntryAt(getFragmentManager()
        .getBackStackEntryCount() - 1)
        .getName();

    return tag;
}

Then, on my onBackPressed() method :

    // Get current Fragment tag
    String currentFrag = getActiveFragment();

    if(currentFrag.equals("PRODUCT_TAG")) {

        // New transaction to first Fragment
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(R.animator.enter_from_right, R.animator.exit_to_left, R.animator.enter_from_right, R.animator.exit_to_left);
        ft.replace(R.id.container, new SearchFragment(), "MAIN_TAG");
        ft.commit();

    } else {

        // Go to Fragment-1
        getFragmentManager().popBackStack();
    }



回答3:


Here is my solution:

Fragment curFragment = fragmentManager.findFragmentById(R.id.frameLayout);
if(curFragment != null
    && curFragment.getClass().equals(fragment.getClass())) return;

// add the fragment to BackStack here

Xamarin.Android (C#) version:

var curFragment = fragmentManager.FindFragmentById(Resource.Id.frameLayout);    
if (curFragment != null
    && curFragment.GetType().Name == fragment.GetType().Name) return;

// add the fragment to BackStack here


来源:https://stackoverflow.com/questions/27957789/prevent-the-same-fragment-to-be-added-multiple-times-in-popbackstack

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