Skip to content Skip to sidebar Skip to footer

Viewpager With One Page Containing Multiple Fragments "java.lang.illegalargumentexception: No View Found For Id"

In my small Android app, I have a ViewPager with 3 pages, each page contains one fragment, that's all working fine with a FragmentStatePagerAdapter (see earlier question). Now for

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 the ViewPager to hold the fragment. Looking in the Android code (and specifically the FragmentPagerAdapter), I should just use container.getId() to find where to add the Fragment. 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 case savedInstanceBundle is always null, 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...

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""