Fragments, Tab Adapter, and Different Layouts

tabs_fragWhen using Fragments in tabs, the FragmentManager takes care of instantiating, retaining, and reusing Fragments. The problem there is that a tag for a fragment is auto-generated. If we want to reuse that exact Fragment in a different layout (like when the device is rotated), we need to extract the tag and use the same one when re-initializing.

The better way to do this is to create your fragment instance and tag yourself. That way, you can easily get a reference to your fragment later from the fragment manager.

So essentially we need to replicate the inner workings on fragment pager adapter and explicitly add your fragment (and your tag).

Jake Wharton has a great code sample that demonstrates this for using a Fragment Pager Adapter. Building off that, I combined the Fragment Pager Adapter code with a standard Tab Adapter.

Check it out on GitHub!

« PREVIOUS
Building LinkedIn University Pages
NEXT »
Field of Study Explorer - Data Insights for Students