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():
- How to show the last fragment (Fragment 4) in frameLayout on orientation change ?
- Here, Main Activity -> Fragment 1 -> Fragment 2 -> Fragment 3 -> Fragment 4
How to Pass data from Fragment 4 to Fragment 3 ?
Confusion
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.
In general, how many level of nested fragment should be manageable.