ZayYarHtun ZayYarHtun - 2 months ago 6
Android Question

About Fragment Creation

I want to know why it needs to

into bundle. When i swipe to other tabs, is my fragment recreated or is the last created used? Why has the blank constructor been used in this?

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {

public Fragment getItem(int position) {
switch (position)

case 0 : return fragment_zg.newInstance(position + 1);

case 1: return fragment_Uni.newInstance(position+1);

default: return fragment_zg.newInstance(position + 1);



// This is my fragment

public class fragment_Uni extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public fragment_Uni() {}

public static fragment_Uni newInstance(int sectionNumber) {
fragment_Uni uni_fragment = new fragment_Uni();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
return uni_fragment;



I want to know why it needs to putInt into bundle.

If you want to pass some value to a Fragment then you need to use a Bundle which is then set as the arguments for the Fragment. Otherwise, the arguments are not retained on Fragment recreation. If your Fragment does not require arguments or it is not mandatory then the Bundle can be omitted.

When i swipe to other tabs, my fragment is recreated or used last created?

It will use the last created, but if you want to update then you have to refresh your page adapter otherwise the data will not update.

Why did blank constructor has been used in this?

It's the default constructor which is required for Fragment.