-1

I have a fragment with TabLayout, which looks like this: enter image description here

Code, where it is created:

public class CalendarActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_calendar);

    SimpleDateFormat sdf = new SimpleDateFormat("EEE dd-MM", new Locale("en", "GB"));

    TabLayout tabLayout = findViewById(R.id.tabs);
    final ViewPager viewPager = findViewById(R.id.viewPager);
    tabLayout.addTab(tabLayout.newTab().setText("today"));
    tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
    tabLayout.addTab(tabLayout.newTab().setText(sdf.format(new Date(((new Date()).getTime() + 2*86400000)))));
    tabLayout.addTab(tabLayout.newTab().setText(sdf.format(new Date(((new Date()).getTime() + 3*86400000)))));
    tabLayout.addTab(tabLayout.newTab().setText(sdf.format(new Date(((new Date()).getTime() + 4*86400000)))));
    tabLayout.addTab(tabLayout.newTab().setText(sdf.format(new Date(((new Date()).getTime() + 5*86400000)))));
    tabLayout.addTab(tabLayout.newTab().setText(sdf.format(new Date(((new Date()).getTime() + 6*86400000)))));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    PageAdapterCalendar adapter = new PageAdapterCalendar (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

Adapter code:

public class PageAdapterCalendar extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PageAdapterCalendar (FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    switch(position){
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
            fragment = CalendarFragment.newInstance();
            break;
    }
    return fragment;
}

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

}

newinstance() in Calendar fragment:

    public static Fragment newInstance() {
    CalendarFragment fragment = new CalendarFragment();
    return fragment;
}

I need each of my tabs to behave the same and use the same layout, but I only need to send them info about the date (same as it's in their title), so they would get different data from database. I have tried bundles and tags, but it didn't work as I expected - Bundles were added all to each tab and tags are not loaded on onCreate() part (I think?), so they were empty.

gariaable
  • 179
  • 1
  • 3
  • 15

1 Answers1

1

In your newInstance method try to pass in the relevant information for each fragment you are creating as parameters. You just need to pass in what little information is necessary so that fragment knows where to get its data from in your database. Then from there create a bundle (eg mBundle) and put all those values into that bundle and call fragment.setArguments(mBundle). Then in your fragment onCreate method parse that bundle to get your data and go from there.

It seems like a lot of steps but this method ensures that the data persists across all lifecycle events

For an example see Best practice for instantiating a new Android Fragment

chris2112
  • 66
  • 4
  • Thank You, but the problem is, even if is mark somehow those data, based on tabs, I still do not know how to get correct values inside fragment, ex. if my bundle would be ("1", "12/12") - 1 to indicate that it belong to first tab, then inside the tab I am not able to check which one is running currently. – gariaable Nov 27 '18 at 22:09
  • @gariaable if you're looking to see which fragment is currently visible you may want to look into overriding setuservisiblehint: https://developer.android.com/reference/android/support/v4/app/Fragment#setuservisiblehint – chris2112 Nov 28 '18 at 15:52
  • I think it is still not exactly what I need, because I am creating 7 instances of the same fragment, so if I'll add a code which checks if given fragment is visible to user it will always returns true..? The only thing I need is to somehow read the title of the tab, which is currently displayed – gariaable Dec 15 '18 at 13:52