Problem: Whenever I navigate through my BottomNavigation "onCreateView" is only triggered first time once on my green child fragment. However it works flawlessly on my ParentFragment.
Red is the Main Activity with the BottomNavigation. Blue is the ParentFragment with the viewpager inside and the green one is the ChildFragment which changes according to the date. So if I swipe right I get different times for "August 30".
Now if I navigate to "Settings Section" and then navigate again to "Home Section", "onCreateView" is only triggered on blue ParentFragment but not on green ChildFragment. However what I want to achieve is everytime when "onCreateView" is triggered on blue ParentFragment it should also be triggered on green ChildFragment. And if I set "OffscreenPageLimit to 1 it should also be triggered on the Fragments right and left to ChildFragment. I couldn't find a solution for my problem.
That's the code snippet of my blue ParentFragment:
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(requireActivity());
ViewPager2 viewPager = binding.viewPager;
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setCurrentItem(1000, false);
viewPager.post(new Runnable() {
public void run() {
// always start at item 1000, so I can swipe left to previous days and swipe right to next days
viewPager.setCurrentItem(1000, false);
//notifyDataSetChanged doesnt trigger "onCreateView" sadly
sectionsPagerAdapter.notifyDataSetChanged();
}
});
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// If I swipe right Date changes to August 30 and vice versa it changes to August 28
LocalDate pagerdate = LocalDate.now();
LocalDate days = pagerdate.plusDays(position - 1000);
String month2 = days.getMonth().getDisplayName(TextStyle.FULL, Locale.getDefault());
String dayOfMonth2 = String.valueOf(days.getDayOfMonth());
String monthAndDay2 = month2 + " " + dayOfMonth2;
monthDayText.setText(monthAndDay2);
}
});
viewPager.setOffscreenPageLimit(1);
Thats the code of my Adapter:
public class SectionsPagerAdapter extends FragmentStateAdapter {
public SectionsPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@Override
public int getItemCount() {
// I have 2000 items to "pretend" to have unlimited items because nobody will swipe 1000 times to the left or right side
return 2000;
}
@NonNull
@Override
public Fragment createFragment(int position) {
LocalDate pagerdate = LocalDate.now();
LocalDate days = pagerdate.plusDays(position - 1000);
//I am passing the date to my Fragment because the time is set according to the date
return TabTodayFragment.newInstance(days.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
}
If anyone has the same problem I found the solution. I replaced my SectionsPagerAdapter Constructor with
public SectionsPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
Now it works fine!
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.