3 android fragments in viewpager, how to keep them all alive?

Steven Akerfeldt picture Steven Akerfeldt · Jan 11, 2013 · Viewed 9.4k times · Source

I have a viewpager with at least 3 fragments. If the user inputs data into A, then B, then C and goes back to A that data is lost from A.

I think I read somewhere that this has to do with memory management and since fragments only connect to or store adjacent fragments it's killing A when I get too far away. However I wish to retain the life of each fragment even were I to have >3.

How can I tell the viewpager to never kill a fragment unless explicitly directed?

Answer

Marcin S. picture Marcin S. · Jan 11, 2013

use

setOffscreenPageLimit(int limit) 

on the ViewPager object. "It sets the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state."