简体   繁体   中英

Replace Current fragment with another fragment from within the Current fragment

I have an activity named as

MainActivity

i have added a fragment " BenefitFragment " in container R.id.mainContainer .

In BenefitFragment layout i have another container R.id.benefitContainer . I am adding a nested fragment as

getChildFragmentManager().beginTransaction()
                        .add(R.id.benefitContainer, new ConfirmPinFragment())
                        .setCustomAnimations(R.anim.slide_in_left, R.anim.do_nothing)
                        .addToBackStack("benefit")
                        .commit();

In my ConfirmPinFragment after some processing i want to remove this ConfirmPinFragment and replace it with another Fragment let's say TestFragment and TestFragment can replace itself with another fragment and so on.

Here is what i tried in ConfirmPinFragment

CorporateFragment fragment = new CorporateFragment();
                    getFragmentManager().beginTransaction()
                                .addToBackStack("benefit")
                                .hide(ConfirmPinFragment.this)
                                .add(android.R.id.content, fragment)
                                .setCustomAnimations(R.anim.slide_in_left, R.anim.do_nothing)
                        .commit();

However i am getting error android.R.id.content not found . I want to replace the fragment from inside it and replace with another fragment, how will i do it.

I see BenefitFragment will be your parent Fragment for both ConfirmPinFragment and CorporateFragment .

So pass R.id.benefitContainer as a container of your fragment in which your fragment to be replaced. But you need to resolve that id first. to do so you need to use getParentFragment() .

Try to write this in your ConfirmPinFragment .

Fragment mF = getParentFragment();
// double check
if (mF instanceof  BenefitFragment) {
     getFragmentManager().beginTransaction()
                    .add(((BenefitFragment)getParentFragment()).getView().findViewById(R.id.benefitContainer).getId()
                            , new CorporateFragment())
                    .setCustomAnimations(R.anim.slide_in_left, R.anim.do_nothing)
                    .addToBackStack("benefit")
                    .commit();
}

As per your requirement, BenefitFragment is your parent fragment and you added ConfirmPinFragment successfully as it's child fragment. Now other fragments let's say TestFragment Or CorporateFragment etc are child fragment of BenefitFragment same as of ConfirmPinFragment. So you have to do same as you had done with ConfirmPinFragment adding code. Just replace add method with the replace .

getChildFragmentManager().beginTransaction()
                        .replace(R.id.benefitContainer, new CorporateFragment())
                        .setCustomAnimations(R.anim.slide_in_left, R.anim.do_nothing)
                        .addToBackStack("ConfirmPin")
                        .commit();

And your layout container for all child fragment will be R.id.benefitContainer

try this code:

     getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, tag).addToBackStack(tag).commit();


//"content_frame" is the name of your view where the Fragment will be shown and //"fragment" is the name of your Fragment

the above code will replace your Fragment

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