why

There are several possible reasons for switching between fragments without data.

The problem I encountered was passing in a FragmentManager object when the FragmentPagerAdapter was initialized.

There are other situations on the Internet that I haven’t encountered yet, so I won’t discuss them yet.

Fragment

We all know there are two fragments

android.support.v4.app.Fragment
Copy the code

and

android.app.Fragment
Copy the code

The difference between v4 and APP packages

  • Fragments in V4 package are compatible with lower versions up to 1.6
  • The Fragment in the APP package can be used in a later version
  • If you use the V4 package, your Activity inherits itFragmentActivity

    import android.support.v4.app.FragmentActivity;
  • If you use the app package, you can inherit the Activity

To solve

There is a problem with passing the FragmentManager object when the FragmentPagerAdapter is initialized.

There are three types of FragmentManger objects:

  • getSupportFragmentManager()
  • getFragmentManger()
  • getChildFragmentManger()

Their application scenarios are different

Use V4 package, will cooperate with getSupportFragmentManager use.

To use app packages, use with getFragmentManager.

GetChildFragmentManger is used when a Fragment is nested within a Fragment.

ViewPager+FragmentPagerAdapter+Fragment(v4)

ViewPager loads the next page by default. SetOffscreenPageLimit () setOffscreenPageLimit(0) has the same effect as setOffscreenPageLimit(1).