简体   繁体   中英

Viewpager with 3 fragments using FragmentPagerAdapter

I have written a program using ViewPager and 3 fragments. My problem is:

When the activity is created first 2 fragments are called. While going from fragment 1 to 2 , when the fragment 2 is clicked or scrolled, fragment 3's newInstance() method is called. And, when I move backwards from fragment 3 to fragment 2 to fragment 1 onCreateView() method is called.

My Code:

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {

private static int NUM_ITEMS = 3;
Context context;

public ViewPagerAdapter(FragmentManager manager,Context context) {
    super(manager);
    this.context = context;
}

@Override
public int getCount() {
    return NUM_ITEMS;
}

// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return Fragment_About.newInstance("About",20);
        case 1: 
            return Fragment_EditSkills.newInstance ("Skills",30);
        case 2:
            return Fragment_EditActivities.newInstance("Activities",40);

        default:
            return null;
    }
}
}

Fragment_About.java

public class Fragment_About extends Fragment {

public static Fragment_About newInstance(String name, int age) {
    Bundle bundle = new Bundle();
    bundle.putString("name", name);
    bundle.putInt("age", age);

    Fragment_About fragment = new Fragment_About();
    fragment.setArguments(bundle);

    Log.i("FragmentAbout instance","instance");
    Log.i("name",""+name);
    Log.i("age",""+age);
    return fragment;
}

private void readBundle(Bundle bundle) {
    if (bundle != null) {
        name = bundle.getString("name");
        age = bundle.getInt("age");
        Log.i("readBundle oncreateview","readBundle oncreateview");
        Log.i("name",""+name);
        Log.i("age",""+age);
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
    container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_about, container, 
    false);

    txtEditAbout = (TextView) view.findViewById(R.id.textEditAbout);
    about = (TextView) view.findViewById(R.id.about);

    readBundle(getArguments());

}
}

Log:

// when the activity is created.
I/FragmentAbout instance: instance
I/EditSkills instance: instance


// When I click on page 2
I/FragActivity instance: instance

Moving Backwards from 3 to fragment 2 to fragment 1 is called but this time the newInstance() is not called for any of the fragments

I/readBundle oncreateview: readBundle oncreateview

Please explain the above process.

Viewpager preloads neighbour fragments depending on OffscreenPageLimit parameter. By default it is set to 1 ( DEFAULT_OFFSCREEN_PAGES = 1)

You used FragmentPagerAdapter , that means that fragment view could be destroyed if they are farther then offscreen limit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM