I have a bottom menu and a tablayout, when I start the application the tabLayout works correctly as you can see in the photo
But when I select a button from the lower activity below and go back to where I was, it happens that the fragments are gone and nothing appears.
I don't understand why this happens to me, this is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_unete, container, false);
tablayout = view.findViewById(R.id.tab_layout);
viewpager = view.findViewById(R.id.view_pager);
//initialize adapter
adapter = new
MainAdapter(getActivity().getSupportFragmentManager());
//add fragment
adapter.AddFragment(new OneUnete(), "Conqueror's Blade");
adapter.AddFragment(new TwoUnete(), "Warframe");
//set adapter
viewpager.setAdapter(adapter);
//connect tab layout with view pager
tablayout.setupWithViewPager(viewpager);
return view;
}
private class MainAdapter extends FragmentPagerAdapter {
//iniciar array list
ArrayList<Fragment> fragmentArrayList = new ArrayList<>();
ArrayList<String> stringArrayList = new ArrayList<>();
//create constructor
public void AddFragment(Fragment fragment, String s){
//add fragment
fragmentArrayList.add(fragment);
//add string
stringArrayList.add(s);
}
public MainAdapter(@NonNull FragmentManager fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment getItem(int position) {
//return fragment
return fragmentArrayList.get(position);
}
@Override
public int getCount() {
//return fragmentlist size
return fragmentArrayList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return stringArrayList.get(position);
}
here the xml of join
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".UneteFragment">
<!-- TODO: Update blank fragment layout -->
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@color/purple_700"
app:tabIndicatorColor="@color/white"
tabmode="fixed"
app:tabIndicatorHeight="3dp"
app:tabSelectedTextColor="@color/white"
app:tabIndicatorAnimationMode="elastic"
app:tabIndicator="@drawable/tab_indicator"
/>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tab_layout"
android:id="@+id/view_pager"/>
</RelativeLayout>
You're not actually using a container for the Fragments and you're not actually performing a transaction to add them.
Check out Snippets
In the layout where you want to carry out the transaction you must have a
FrameLayout
which you would use as a container, example:To add Fragments you must perform a transaction (using
FragmentTransaction
), define the class ofFragment
and the container where it will be performed.Example assuming your Fragment class is called
MyFragment
:I solved it by changing the way to put the tabLayout using the viewPager2
Start the variables
In the onCreateView I put
I create the FragmentAdapter class
So I have the XML of the fragment
UneteFragmento