简体   繁体   中英

how to replace existing fragment in a fragment manager with another fragment based on tag

i have an activity that will show data using fragment, for each fragment i give the unique id as the Tag like this:

public class MyActivity .... {

    void addFragment(String id){
        // this is how i add a new fragment
        FragmentTransaction fTrans = getFragmentManager().beginTransaction();

        AnyFragment frag = new AnyFragment();

        fTrans.add(R.id.container, frag, id);
        fTrans.commit();
    }

    void replaceFragment(String id){
        // this is how i replace the fragment with tag 'id' with new fragment
        FragmentTransaction fTrans = getFragmentManager().beginTransaction();

        AnyFragment newFrag = new AnyFragment();
        fTrans.replace(R.id.container, newFrag, id);
        fTrans.commit();
    }

}

In my example i have add 2 fragment with tag ' data1 ' and ' data2 '.

When i'm tried to replace the fragment with tag ' data1 ' or ' data2 ', it always replacing the fragment which added first and when i'm replace for the second time, it replace the second added fragment.

When i'm replacing for 3 times, it leave me only one fragment and one other fragment is gone.

My problem is:

I want to replace the fragment with specified tag with a new fragment, how to do that? There's something wrong for replace in my code?

Can i use replace method? or i must use remove the fragment with specified tag first then add the new fragment with same tag? What's the best approach to do that? It would be great if you can give some explanation, for each approach.

Any answer would be appreciated, thanks!

You don't really need tags in your case:

Check the method description on Android Developer:

Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

So all you do is:

  1. During onCreate() of Activity (or whenever you actually need to instantiate a fragment) - fill the container with a new fragment: getSupportFragmentManager().beginTransaction().replace(R.id.container, Fragment.instantiate(this, FragmentA.class.getName())).commit();

  2. Once you need to replace FragmentA with FragmentB, you just call getSupportFragmentManager().beginTransaction().replace(R.id.container, Fragment.instantiate(this, FragmentB.class.getName())).commit(); (ie to tags required - R.id.container has only one fragment at the same time)

If you don't want to replace fragments (which is preferred solution), you can in onCreate() of your activity add() both fragments, and then use show() and hide() methods respectively:

    Fragment fragmentA = Fragment.instantiate(this, FragmentA.class.getName());
    Fragment fragmentB = Fragment.instantiate(this, FragmentA.class.getName());
    ..
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, fragmentA)
            .add(R.id.container, fragmentB)
            .show(fragmentA)
            .hide(fragmentB)
            .commit();

    ..
    // Once you want to show fragment B instead of fragment A
    getSupportFragmentManager().beginTransaction()
            .show(fragmentB)
            .hide(fragmentA)
            .commit();

UPD: regarding editing of the existing fragment:

In your Activity, once something happened and you want to update the fragment - do this:

  1. In onCreate() (or any other suitable place) you add fragment into container( R.id.container ): getSupportFragmentManager().beginTransaction().replace(R.id.container, Fragment.instantiate(this, FragmentA.class.getName())).commit();

  2. In the place where you want to update - do this:

     Fragment f = getSupportFragmentManager().findFragmentById(R.id.container); if (f instanceof FragmentA) { // doSomething() is a public method of FragmentA; ((FragmentA)f).doSomething(); } 

I hope, it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM