Viewpager With One Page Containing Multiple Fragments "java.lang.illegalargumentexception: No View Found For Id"
Solution 1:
All right, even though I moved to another way of doing things, I don't like to leave things incomplete...
Going the way of the nested Fragments as suggested by Mark. The link posted in the comments goes to 404, but I found your example code about nested fragments.
A few things I've learned on the way:
Nested fragments cannot have
setRetainInstance(true)
, or it will get:java.lang.IllegalStateException: Can't retain fragements that are nested in other fragments
It was a mistake to create a
FrameLayout
in the page of theViewPager
to hold the fragment. Looking in the Android code (and specifically theFragmentPagerAdapter
), I should just usecontainer.getId()
to find where to add theFragment
. Looks like you cannot do anything more sophisticated at that level or it will break.From my understanding, if the fragments hold some information (like I do) that takes a long time to load, the different ways to handle this are:
- Use
onSaveInstanceState
(preferred way ?), but not possible if the Fragment is nested. The (empty) constructor will get called when needed by the framework. - Use
setRetainInstance(true)
, in that casesavedInstanceBundle
is alwaysnull
, but that Fragment (as Java object) will not be destroyed and it will still go through onAttach / onCreateView. - Find some other way to save and restore what you need...
- Use
Anyway, I pushed my solution using nested fragments on github
Post a Comment for "Viewpager With One Page Containing Multiple Fragments "java.lang.illegalargumentexception: No View Found For Id""