0

I have a Fragment, in which I have a nested fragment. I've attached an image to illustrate. So I have child a nested, when I click button 1, I replace child A with child B, then on button 2 click I replace child B with child C. Now when I click on button 3, I replace the parent (Fragment 1 with Fragment 2), this is what I want to do.

When I hit the back button when on Fragment 2, I pop the backstack and I display fragment 1, the problem is child A is displayed, I need to figure out how to display child c when I go from Fragment 2 to Fragment 1. I need to mention also that child c contains test results that are displayed in a grid view. Can someone help me do this please?

EDIT

Below is the code I'm using for the transactions for the child fragments (button 1 and button 2 click)

 protected void nextNestedFragment(Fragment nestedFragment){
        FragmentTransaction ft = getParentFragment().getChildFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.animator.enter_slide_in,R.animator.enter_slide_out,R.animator.close_slide_in, R.animator.close_slide_out);
        ft.replace(R.id.nested_fragment_container, nestedFragment).addToBackStack(null).commit();    
    }

So for the above I pass in the next fragment I wish to navigate to within the parent fragment. Below is the code I use on button 3 press to navigate from Fragment 1 to Fragment 2:

  protected void nextFragment(Fragment nextFrag){
        FragmentTransaction ft = getParentFragment().getFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.animator.enter_slide_in,R.animator.enter_slide_out,R.animator.close_slide_in, R.animator.close_slide_out);
        ft.replace(R.id.fragment_container, nextFrag).addToBackStack(null).commit();
    }

enter image description here

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • did you add the other transaction to the backstack? – Blackbelt Mar 05 '15 at 18:07
  • I added all transactions to the backstack, so the child transactions and the transactions of the other fragments. If that makes sense? So basically all transactions are added to the backstack – DJ-DOO Mar 05 '15 at 18:34
  • Could you please provide code that changes child fragments on button 1/2/3 press? – Ayzen Mar 05 '15 at 18:44
  • @Ayzen I have edited my original post to include the code I use for child fragment and parent fragment – DJ-DOO Mar 05 '15 at 20:03

1 Answers1

1

First, you need to know, that you have 2 different FragmentManager here (the default one, and ChildFragmentManager) and each with it's own back stack. And when you press a back button, you pop a back stack of your first FragmentManager, which shows you Fragment 1.

Second, when Fragment 1 is being popped from back stack onCreateView of that fragment is invoked. And I'm pretty sure you are creating a view there with a child A inside.

What you need to do is to save Fragment's state. There are a lot of questions here on how to do this correctly. Start from here.

Community
  • 1
  • 1
Ayzen
  • 933
  • 7
  • 10
  • you are correct, in Fragment 1 oncreate I am adding my child A like so `getChildFragmentManager().beginTransaction().add(R.id.nested_fragment_container, sensorGridFragment).commit();` So in order for Fragment 1 to display child C when the back button is press from Fragment 2 I need to save the Fragments state? Do I do this on button 3 press, before I add the transaction to the backstack? Sorry if the question sounds silly, it's my first time around this – DJ-DOO Mar 05 '15 at 21:14
  • As a workaround you need an instance variable in Fragment 1 with info on which child is currently attached to fragment. And in onCreateView you should check this variable and add corresponding child. It's okay to use instance variable here since your Fragment instance is not destroyed in back stack. – Ayzen Mar 05 '15 at 21:25
  • This variable you should change every time you change nested child. – Ayzen Mar 05 '15 at 21:26
  • the problem is, I only add that child in Fragment 1, then the remaining transactions take place within the child fragments (hence the two different FragmentManagers), could you show me an example, as at the moment I'm not following...I've been staring at this for a long time so my brain is slightly numb! – DJ-DOO Mar 05 '15 at 21:34
  • You have getParentFragment method, so you can set this variable from your child fragments using some set method. Something like this: getParentFragment().setCurrentChild(2). – Ayzen Mar 05 '15 at 21:40
  • that doesn't work I'm afraid, even if I declare a public method in my parent fragment, getParentFragment.(method) doesn't register for use... I can I suppose have `Fragment1 parent = (Fragment1)getParentFragment();` and set it that way, but I'm still unsure how to save the state... – DJ-DOO Mar 05 '15 at 21:44
  • You need to cast the result of getParentFragment to your Fragment 1 class. ((MyCustomFragment1) getParentFragment()).setCurrentChild(2) – Ayzen Mar 05 '15 at 21:48
  • I realised that and edited my response, apologies for that. Sorry to ask further questions but even if I set my instance variable, I'm unsure on how to save the state in order to preserve the data in child c and have it displayed in Fragment 1 – DJ-DOO Mar 05 '15 at 21:49
  • This is more complex thing than your initial question. :) You should try to google some info on how to preserve nested fragment's state or ask another question. Very dirty solution is described here: http://stackoverflow.com/a/23533575/2415329 – Ayzen Mar 05 '15 at 22:02
  • Sorry I thought I'd provided all I needed in the question. I was hoping to display child C in Fragment 1 when back button was pressed in Fragment 2....anyway, I guess I'll have to keep digging. Thanks – DJ-DOO Mar 05 '15 at 22:03
  • I pointed out the reason why you don't get the expected result, but I cannot create the whole solution for you. :) Now, when you understand the problem, you can find more relevant information and refactor your code accordingly. And try that dirty solution for Fragment 1. I think it should work. – Ayzen Mar 05 '15 at 22:09
  • We're you ever able to solve your problem, as I am struggling with the exact same problem and was hoping you could share with me how you were able to accomplish it – Michael Hanson Aug 31 '20 at 18:24