Marco D.
Marco D.

Reputation: 133

Why calling a Fragment in onCreateView()?

I have an Activity with a ViewPager. The ViewPager includes 3 Fragments. They can change by a Swipe.

For Example:

  1. FirstFragment + Swipe (right to left) = SecondFragment
  2. SecondFragment + Swipe (right to left) = ThirdFragment
  3. ThirdFragment + Swipe (left to right) = SecondFragment
  4. ...

My Problems:

  1. If I am on the FirstFragment. Just the FirstFragment and the SecondFragment call OnCreateView.

How can I make that every Fragment call onCreateView?

  1. If I change from the ThirdFragment to the SecondFragment, the onCreateView of the FirstFragment is called.

If I understand the Fragment lifecycle, I think at the moment I switch to the ThirdFragment the FirstFragment is calling the method onDestroyView(). How can I avoid that, so that everytime each Fragment is 'live'?

I hope you can help me. Please be specific I am a beginner.

BR Marco

Upvotes: 0

Views: 38

Answers (2)

Shudy
Shudy

Reputation: 7936

Using the Viewpager with FragmentPagerAdapter keeps the fragments in in memory.

When you "load" for first time the fragment, will invoke OnCreate. But the second time Android will look into memory for it, so there is no need to call onCreate agian.

Take a look to the offical documentation: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

Upvotes: 0

Marco D.
Marco D.

Reputation: 133

viewPager.setOffscreenPageLimit(5) works for me

Upvotes: 1

Related Questions