Why does the fragment's onCreateView, onCreate, onActivityCreated are called

点点圈 提交于 2019-11-29 20:11:27

I would change your architecture for this one on the android developer documentation:

http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

but I would change some things...

1-I would change this method:

/**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Fragment #" + mNum);
        return v;
    }

For something like this where we decide which fragment you populate depending the position of the viewPager:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

SupportFragmentManager ft = getChildFragmentManager().beginTransaction();

String tag = "";
Fragment fragment = null;

  switch (mNum) {
  case 0:
    fragment = new MyFragmentZero();
    tag = FragmentTags.TAG_0;
    break;
  case 1:
    fragment = new MyFragmentOne();
    tag = FragmentTags.TAG_3;
    break;
  case 2:
    fragment = new MyFragmentTwo();
    tag = FragmentTags.TAG_2;
    break;
  default:
    break;
  }

/*OPTIONAL We can pass arguments to the fragments
Bundle args = new Bundle();
args.putInt(Arguments.ARG_POSITION, mNum);
fragment.setArguments(args);*/

//Place the fragment in the container
ft.replace(R.id.fragment_container fragment, tag);
ft.commit();

//You need a base layout for all fragment and use nested fragments later or you can define the layout for each position(mNum) inside the switch.
return inflater.inflate(R.layout.fragment_layout_default_for_all_views, container,
    false);
}

Like this you will have a good architecture and once it is working like this should be fine.

Anyway you must know how the viewPager works populating the fragment in the different positions.

When you start on the position 0, then the fragment on the position 0 and the one of the position 1 are created.

Then when you swipe to the position 1 the fragment on the 2 position is created, so you have now the three fragments created on the different positions (0,1,2..assuming you have only 3 pages on the viewPager).

We swipe to the position 2, the last one, and the fragment on the first position (0) get destroy, so we have now the fragments on the positions 2 and 3.

I hope it helped and let me know if you have any problem. Cheers

ViewPager retain in memory 1 page by default, to either side of the current page. So it would not re-create those pages when swiping 1 page left/right of the current page. But when swipe more than 1 pages to left/right, it would re-create those page again, hence called OnCreateView(), OnCreate().

If app uses few pages 3, you can increase the number of pages to retain by calling,

mViewPager.setOffscreenPageLimit(2);

Described here

Finally I was able to figure it out. Just need to override the destroyItem method so that it won't destroy objects. Hope this is going to be useful for someone.

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    Log.d(TAG, "destroy!");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!