After migrating my navigation to the new Jetpack Navigation Component, I found that after navigating back and forth using the the libraries, the content/layout of the first two fragments in my ViewPager disappears. After clicking through the tabs, it comes back though.
At first I thought it was because of the errors that I saw in the logcat, but after fixing them, the problem was still there.
Here is the code of my MainActivity:
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindResources();
// Setup Toolbar & Navigation Component
setSupportActionBar(toolbar);
setupNavigation();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_options, menu);
return super.onCreateOptionsMenu(menu);
}
private void setupNavigation() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(drawerLayout)
.build();
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
Here is the code of my ViewPagerAdapter:
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
public void addFragment(@NonNull Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
This is my HomeFragment:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
bindResources(view);
// ViewPager
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
viewPagerAdapter.addFragment(new TodayFragment(), getString(R.string.tab_today_title));
viewPagerAdapter.addFragment(new PriorityFragment(), getString(R.string.tab_priority_title));
viewPagerAdapter.addFragment(new PlannedFragment(), getString(R.string.tab_planned_title));
viewPagerAdapter.addFragment(new TasksFragment(), getString(R.string.tab_tasks_title));
viewPager.setAdapter(viewPagerAdapter);
// TabLayout
tabLayout.setupWithViewPager(viewPager);
}
Java: I fixed the problem by replacing
= new ViewPagerAdapter(getActivity().getSupportFragmentManager());
with
= new ViewPagerAdapter(getChildFragmentManager());
Kotlin: By replacing
= ViewPagerAdapter(fragmentManager)
with
= ViewPagerAdapter(childFragmentManager)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With