'Why the fragment only shows on create and after tab switch?

So, in the current state, I can see the fragment inside, showing when I boot up the virtual phone; however, as soon as I switch to different fragment, from my drawer, and switch back again into the fragment, with the tableLayout, its shows nothing! Plus, switching between the tabs does seem to fix it.

You can see the image here

Here is the code:

package com.example.teamapp;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;

public class HomeFragmentNav extends Fragment {

    FragmentManager fragmentManagerHome;
    ViewPager viewPager;
    TabLayout tableLayout;
    PagerAdapter pagerAdapter;
    TabItem TeamsTab;
    TabItem ChatTab;
    View v;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_home_nav, container, false);

        tableLayout = v.findViewById(R.id.TabBar);
        TeamsTab = v.findViewById(R.id.TeamsTab);
        ChatTab = v.findViewById(R.id.ChatTab);
        viewPager = v.findViewById(R.id.ViewPager);
        tableLayout.getTabAt(1).select();
        tableLayout.getTabAt(0).select();
        fragmentManagerHome = getFragmentManager();
        pagerAdapter = new PagerAdapter(fragmentManagerHome,
            tableLayout.getTabCount(), getContext());
        viewPager.setAdapter(pagerAdapter);
        tableLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setAdapter(pagerAdapter);
                viewPager.setCurrentItem(tab.getPosition());

            }

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

            }

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

            }
        });
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        tableLayout = v.findViewById(R.id.TabBar);
        TeamsTab = v.findViewById(R.id.TeamsTab);
        ChatTab = v.findViewById(R.id.ChatTab);
        viewPager = v.findViewById(R.id.ViewPager);
        tableLayout.getTabAt(1).select();
        tableLayout.getTabAt(0).select();
        fragmentManagerHome = getFragmentManager();
        pagerAdapter = new PagerAdapter(fragmentManagerHome,
            tableLayout.getTabCount(), getContext());
        viewPager.setAdapter(pagerAdapter);
    }
}

Thank you for your help.



Solution 1:[1]

Prevent the page from refreshing.

view_pager.setOffscreenPageLimit(2);

Solution 2:[2]

Found the problem, I used getFragmentManager() instead of getChildFragmentManager()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hamid-Ghasemi
Solution 2 Aldan