Shures
Shures

Reputation: 25

How to remove Current added child fragment from another fragment?

I have 3 fragments like fragment A , Fragment B, Fragment C. Initially Fragment A is loaded and Fragment A adds Fragment B , Similarly Fragment B again adds Fragment C.Now When I click backpress or any buttoms in Fragment C , it shows directly fragment A. Why it is not showing Fragment B ? I just want to remove the current fragment c and show fragment b but how please help me .here is my code.

//This is FragmentA and it adds FragmentB
 fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.activity_main,new FragmentB());
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

//This is FragmentB and it adds FragmentC
FragmentManager fragmentManager = getChildFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.publicCommunication,new FragmentC());
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

//This is FragmentC and button is available remove cuurent fragment. now here is my problem , it removes both fragmentC and FragmentB and Shows FragmentA. I want it to show FragmentC but it is not showing it shows fragmentA.
 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager = getActivity().getSupportFragmentManager();
                 manager.popBackStackImmediate();
            }
        });

Upvotes: 0

Views: 4389

Answers (4)

Naresh NK
Naresh NK

Reputation: 1066

Since you are adding FragmentC in the childFragmentManager of FragmentB, you have to remove/pop the FragmentC from FragmentB's childFragmentManager (not from Activity's supportFragmentManager).

So in your Button click listener just do this -

kotlin :

parentFragment?.childFragmentManager?.popBackStack()

Java :

getParentFragment().getChildFragmentManager().popBackStack()

Upvotes: 0

Biplob Das
Biplob Das

Reputation: 3104

Use this,

getFragmentManager().popBackStackImmediate();

Instead of

FragmentManager manager = getActivity().getSupportFragmentManager();
manager.popBackStackImmediate();

bacause, it should use only from an activity and not from fragments.

Upvotes: 0

Ayush Khare
Ayush Khare

Reputation: 1842

You are calling the activity fragment manager to pop the backstack but it does not seem this manager "propagates" the pop to children managers

Change your onClick code as below

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager manager = getFragmentManager();
             manager.popBackStackImmediate();
        }
    });

Upvotes: 3

Nikhil Jadhav
Nikhil Jadhav

Reputation: 1621

in your code, you are not adding any fragment to backstack, you are only adding fragment to an activity, so you need to change this line:- fragmentTransaction.addToBackStack(null); with this :-fragmentTransaction.addToBackStack("With your unique string");

add this to your both fragment transactions. and then commit.

Upvotes: 0

Related Questions