I've got a viewPager inside a fragment (A). Inside the viewPager there are 3 another fragments. If I replace the fragment A with fragment B and click back, the viewPager appears with all fragments being empty. Scrolling back and forward reloads the first and third fragments.
I googled a lot, some people suggested to pass the childFragmentManager to the FragmentStatePagerAdapter, but if I do this, the app crashes with "
No view found for id ** for fragment **" exception.
I tried to use the FragmentPagerAdapter - same result.
Did anyone come across with this problem and has a suggestion?
Ok, I figured that out. The problem with passing the childFragmentManager was because child fragment doesn't contain the container whose fragment(s) are to be replaced, so the app crashed with "No view found..." exception. However, when replacing the fragment, if I use the parent's fragmentManager, then all works fine! So, instead of fragment.fragmentManager!!.beginTransaction() I now call fragment.parentFragment!!.fragmentManager!!.beginTransaction(). In case someone will face the same problem.
Related
As this Question is Many times asked but in my case the scenario is like on orientation change the view pager keeps the fragments of previous orientation.I tried to solve it by passing getChildFragmentManager() to the FragmentStatePagerAdapter but in that case it is showing only first fragment after scrolling it is showing "BLANK" pages ,still first fragment is having data.
Also i tried changing FragmentPagerAdapter and added destroyItem() also tried by overriding getItemPosition() but no success.
Any help will be greatly appreciated.Thanks.
I am using a FragmentStatePagerAdapter in combination with a view pager, which is nested in a fragment on Android. Thus, I am using the ChildFragmentManager in combination with the FragmentStatePagerAdapter to create my fragments.
So far so good, but here is the problem:
If I open a different Activity without finishing the activity that hosts the fragment with the view pager and afterwards resume it, the fragment that was selected in the ViewPager is empty. If I switch the fragment in the ViewPager before I open the different Activity, this issue does not appear. After returning and seeing the empty Fragment in the ViewPager, I am still able to switch the next Fragments, but if I try to switch back to the empty Fragment, I get a NullPointerException. This issue does not occur if I switch between the Fragment hosting the ViewPager and a different Fragment or if I change orientations.
If I use the FragmentManager instead of the ChildFragmentManager, I do not have this issue when switching to a different Activity, but it will then occur if I switch between the hosting and a different fragment or if I change the orientation.
So here's what I already tried without any success:
* Use FragmentStatePagerAdapter adapter and FragmentPagerAdapter
* Use setRetainedInstance(true and false)
* Use getChildFragmentManager() and getFragmentManager()
* Override getItemPosition() return POSTION_NONE and use notifyDataChanged() in onResume()
The only thing that is currently doing the trick is recreating the PagerAdapter in onResume() and re-initializing the ViewPager, but I think that's really bad practice as everything needs to be recreated.
So is there somebody, who can explain to me how a correct implementation should look like, or why the fragments can become empty (they are not null, I checked that). I am searching for a more elegant solution rather than recreating everything each time I resume my activity.
Any help is really appreciated.
Scenario:
I have a fragment which has a ViewPager which contains 5 instances(different) of a single Fragment with different values, each having a listivew that contains some sort of items.
Problem:
The flow goes smooth when I click an item of listView(say on page1) but the moment I come back from there on pressing back (overridden OnBackPressed in the main_activity (will discuss later)), the viewPager fails to load the subFragments.
But when I switch to the 2nd or 3rd page, it gets displayed, and on going back to 1st page, it now gets displayed.
OnBackPressed():
I am maintaining a manual stack here. When I click on an item of ListView, the current Fragment Instance (of the parent Fragment ofcourse) goes into stack. And when user comes back , that instance gets popped off the stack and I replaces it on the activities FrameLayout container.
References:
Fragments not getting recreated by FragmentStatePagerAdapter after coming back to the main Fragment instance
Guys, I am really pissed off here, please help me out.
Frankly, I haven't worked much with fragments.
But it seems like the page is being thrown off the memory every time you switch to other pages. I am not sure but if there is a way for android to manage this and keep the page in its memory persistently, it might solve the problem
Finally I am able to get a workaround for this issue, which included two things:
while replacing the fragment make a call to
ft.replace(R.id.content_frame, frag).addToBackStack(null);
while initiating the ViewPager, the fragment manager to be used is :
mAdapter = new myAdapter(getChildFragmentManager());
Actually the problem is that, android fails to recognise and load the older instance of the fragment unless you explicitly add that instance to the backstack. So after adding it to BackStack, there is practically no need to maintain your own manual Stack, but you still can do that.
I have an activity which hosts 3 fragments in a ViewFlipper. Each of those three fragments hosts fragments of their own. I am using the ViewFlipper like a tab control, which allows me to very simply switch between various "views" in the app. This all works fine, so far.
When the user is inside a view, there is a navigation flow. I use:
final FragmentTransaction txn = getChildFragmentManager()
.beginTransaction();
txn.replace(R.id.view1_silo_container,
new View1Fragment());
txn.addToBackStack(null);
txn.commit();
to move around inside this view. So as the user navigates, I call some variation of the code above to replace the current fragment with a new one. Again, this all works fine so far.
The problem is that, when I get to the bottom fragment (A>B>C) and then I hit the back button to go from (C>B) I get a duplicate id error. The problem is that the "B" fragment itself has a fragment nested in it. As long as I avoid giving this fragment an id, there is no problem. However, if I give this fragment an id then I get "Duplicate id, tag null, or parent id 0x0 with another fragment".
I don't understand why this is a problem, and I haven't found a way to work around it. Am I doing it completely wrong? Is there some small piece I am missing?
The answer is:
Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.
This can be found in the Android 4.2 APIs documentation about the new nested fragments feature.
Once I removed the <fragment> from the layout and used getChildFragmentManager() to insert the fragment 'manually' in onCreateView, it worked fine. No more duplicate errors.
I'm having a bit of trouble implementing a design based around multiple ViewPagers.
At a high level, I have a FragmentActivity with just a FrameLayout as it's content. I have 3 different Fragments that I want to display. All 3 are full screen and only 1 will be used at a time.
Fragment 1 is a basic fragment with some TextViews and ImageViews.
Fragment 2 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments.
Fragment 3 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments (that are different from Fragment 2)
In my FragmentActivity onCreate() I get the FragmentManager and begin a transaction to replace whatever is in my FrameLayout with a new instance of Fragment 2.
At this point everything is working as expected. My ViewPager in Fragment 2 works perfectly.
Now I have a menu option that replaces the Fragment 2 in my FrameLayout with a new instance of Fragment 3. This also works fine.
The problem arises when I try to put Fragment 2 back into the FrameLayout with another replace transaction. I see my PagerIndicater at the top, but my pages are blank.
I've tried just creating a new instance of my Fragment 2 and calling a replace transaction. I've also tried setting a tag on my Fragments when I call replace and adding a findFragmentByTag check before my replace instead of creating a new instance. Both gave me the same result of blank pages after my second replace.
For reference
My first design was simply a FragmentActivity with a ViewPager and a ViewIndicater. I only had Fragment 2 and Fragment 3 from my description above and a menu option to switch between them. To switch I had 2 different FragmentPagerAdapters defined and just called ViewPager.setAdapter to set the selected FragmentPagerAdapter. This was working perfectly, but now I need a new top level Fragment that isn't using ViewPager at all. This is why I decided to move my ViewPagers out into their own Fragments. My idea being that I would just swap in my fragments to a FrameLayout.
I don't have my code in front of me right now so I can't post any, but I'll add some code to my question tomorrow to help facilitate answers.
This question is a possible duplicate of Navigating back to FragmentPagerAdapter -> fragments are empty.
If your app can handle it (API 17), use getChildFragmentManager(). This problem seems to occur when using a Fragment to host your ViewPager and using FragmentPagerAdapter. Changing to FragmentStatePagerAdapter seemed to fix the problem as well, but I still think using getChildFragmentManager() is the smartest thing to do.
brockoli you can used not "good way". But for my it's worked.
You can use in view 2 layouts, where you bind fragments. First - for fragment with fragments. Second - for other fragments.
On replace fragment with fragments do not replace, but only change visibility first layout to gone and add new fragment to second layout. On back, remove fragment from second layout and set visibility for first layout to visible.