1

For my current app, I am using minSdkVersion 14 with fragment support library for creating fragments.

Issue(Multiple Dynamically Created Nested Fragment):

In other apps that I have created there is only one or two level of dynamic fragments that I managed by creating the interface in fragments and then implementing that interface in my activity.

But for my current app, I need to implement 3 or 4 level of dynamically created fragment as

MainActivity has a frameLayout,
then from main Activity Fragment 1 is called and added to frameLayout
then from Fragment 1 Fragment 2 is called and again replaces the content of same frameLayout... and so on till finally fragment 4 replaces the content of same frameLayout.
On each page(fragment Layout) from Fragment 1 to Fragment 4, user has to select something in order to go to the next page.

It's working fine on first run and everything comes properly but on changing the orientation of device only first fragment (Fragment 1) is added frameLayout, instead of last one(Fragment 4) that fill frameLayout in last.

I have added this.setRetainInstance(true); to all my fragments though.

I am calling new fragment with current one using:

AllocationFragment.NewInstance(CurrActivityContext, urlToCatch2);
Here, CurrActivityContext = MainActivity.this

and then in newInstance(): enter image description here

  1. How to show the last fragment (Fragment 4) in frameLayout on orientation change ?
  2. Here, Main Activity -> Fragment 1 -> Fragment 2 -> Fragment 3 -> Fragment 4
    How to Pass data from Fragment 4 to Fragment 3 ?

Confusion

  1. Is it good idea to add multiple level (like 4 in my case) of dynamically created fragments or after every fragment I should pass the data back to the main activity, which will act as a controller and transfer the data to next fragment.

  2. In general, how many level of nested fragment should be manageable.

Uday Shankar Singh
  • 531
  • 1
  • 5
  • 17

2 Answers2

0

1- How to show the last fragment (Fragment 4) in frameLayout on orientation change ?

you can use your activity to save the index of current fragment.

  int currentFragmentIndex;

  @Override protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("current_fragment_index", currentFragmentIndex);
  }

  @Override protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    currentFragmentIndex = savedInstanceState.getInt("current_fragment_index", 0);
    //replace content layout with your fragment 
  }

2- Here, Main Activity -> Fragment 1 -> Fragment 2 -> Fragment 3 -> Fragment 4 How to Pass data from Fragment 4 to Fragment 3 ?

you seems handling it correctly by passing the values to the activity, but there is another way that I don't recommend is using Fragment.setTargetFragment and retrieve the target fragment by using Fragment.getTargetFragment and pass data using fragment reference.

3- Is it good idea to add multiple level (like 4 in my case) of dynamically created fragments or after every fragment I should pass the data back to the main activity, which will act as a controller and transfer the data to next fragment.

I think this is the best way to do it.

4- In general, how many level of nested fragment should be manageable.

I don't think there is a limitation on that.

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
  • 1
    Finally got this right.. As you said, I was doing right by using activity as controller, but passing data directly from fragment 4 to fragment 3 is not a good idea as it may lead to serious app crashes(as in my case), but once I passed the data using activity ... everything works.. even my device rotation handling code, which was killing me earlier.. Thanks a lot – Uday Shankar Singh Apr 01 '15 at 11:41
0

MoshErsan, thanks for replying but regarding point 1, I am not creating fragment 4 from main activity. The flow is like main activity creates fragment 1 which replaces framelayout then from fragment 1 fragment 2 is created on the fly which then again replaces framelayout then from fragment 2 fragment 3 is created on the fly which again replaces framelayout and finally fragment 4 is created dynamically from fragment 3 which finally shows data after again replacing same framelayout. So how main Activity will get the index of fragment that is not created by it instead created by some fragment. Also all these fragments are replacing the same framelayout.

Please do tell if some more of my code is required to make things more clear...

Uday Shankar Singh
  • 531
  • 1
  • 5
  • 17
  • as you said, you are using the activity as controller by passing the values between the fragments, then you can use the activity to create the fragments! – Mohammad Ersan Mar 22 '15 at 19:13
  • yes that's correct I am using activity as controller, if the nested level is of 2 but here I have nesting till 4th level, and here my 3rd fragment is creating my 4th fragment and activity has no idea about that. Then how to pass data back from 4th fragment to 3rd fragment. – Uday Shankar Singh Mar 23 '15 at 04:49