androidandroid-fragmentsandroid-viewpagerandroid-fragmentactivity

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


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?


Solution

  • use

    setOffscreenPageLimit(int limit) 
    

    on the ViewPager object.

    From the documentation:

    [It set]s the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.