So, im combining BottomNavbar with Tablayout, its works fine at first but when im changing menu from bottom navigation bar from home
and then back to menu
again , one of tablayout fragment is gone
like this :
as you can see in the image, at first its working perfectly fine, but when im changing to home then back in menu again, the fragment
of tab minuman
is gone
here is my code :
Layout
fragment_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MenuFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayoutView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/pal1c3"
app:tabTextColor="@color/white"
app:tabIndicatorColor="@color/black"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/containerViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
android:layout_below="@+id/appBarLayout"/>
</RelativeLayout>
</FrameLayout>
fragment_makanan_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewMenuHome.MakananMenuFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:layout_gravity="center"
android:gravity="center"
android:textSize="30sp"
android:text="Hello text"/>
</FrameLayout>
fragment_jajanan_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewMenuHome.JajananMenuFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/black"
android:textSize="30sp"
android:text="Jajanan Menu Fragment" />
</FrameLayout>
fragment_minuman_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewMenuHome.MinumanMenuFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/black"
android:textSize="30sp"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
Java
MenuFragment.java
package com.example.pesanpalgading20;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.tabs.TabLayout;
/**
* A simple {@link Fragment} subclass.
* Use the {@link MenuFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class MenuFragment extends Fragment {
TabLayout tabLayout;
ViewPager viewPager;
Context context;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public MenuFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment MenuFragment.
*/
// TODO: Rename and change types and number of parameters
public static MenuFragment newInstance(String param1, String param2) {
MenuFragment fragment = new MenuFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_menu, container, false);
tabLayout = (TabLayout)viewRoot.findViewById(R.id.tabLayoutView);
viewPager = (ViewPager)viewRoot.findViewById(R.id.containerViewPager);
context = container.getContext();
tabLayout.addTab(tabLayout.newTab().setText("Makanan"));
tabLayout.addTab(tabLayout.newTab().setText("Minuman"));
tabLayout.addTab(tabLayout.newTab().setText("Jajanan"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final MyAdapter adapter = new MyAdapter(getActivity(),getFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(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) {
}
});
// Inflate the layout for this fragment
return viewRoot;
}
}
MyAdapter.java
package com.example.pesanpalgading20;
import android.content.Context;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import com.example.pesanpalgading20.ViewMenuHome.JajananMenuFragment;
import com.example.pesanpalgading20.ViewMenuHome.MakananMenuFragment;
import com.example.pesanpalgading20.ViewMenuHome.MinumanMenuFragment;
public class MyAdapter extends FragmentPagerAdapter {
private Context myContext;
int totalTabs;
public MyAdapter (Context context, FragmentManager fm, int totalTabs){
super(fm);
myContext = context;
this.totalTabs = totalTabs;
}
//fragment per tabs
@Override
public Fragment getItem(int position){
switch(position){
case 0:
MakananMenuFragment makananMenuFragment = new MakananMenuFragment();
return makananMenuFragment;
case 1:
MinumanMenuFragment minumanMenuFragment = new MinumanMenuFragment();
return minumanMenuFragment;
case 2:
JajananMenuFragment jajananMenuFragment = new JajananMenuFragment();
return jajananMenuFragment;
default: return null;
}
}
//counts total number of tabs
@Override
public int getCount() {
return totalTabs;
}
}
MakananMenuFragment.java
package com.example.pesanpalgading20.ViewMenuHome;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import com.example.pesanpalgading20.R;
import com.example.pesanpalgading20.ToppingAdapter.ToppingAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Use the {@link MakananMenuFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class MakananMenuFragment extends Fragment {
ExpandableListView expandableListTopping;
List<String> listGroup;
HashMap<String,List<String>> listItem;
ToppingAdapter toppingAdapter;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public MakananMenuFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment MakananMenuFragment.
*/
// TODO: Rename and change types and number of parameters
public static MakananMenuFragment newInstance(String param1, String param2) {
MakananMenuFragment fragment = new MakananMenuFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
public MakananMenuFragment(String mParam1) {
this.mParam1 = mParam1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_makanan_menu, container, false);
// Inflate the layout for this fragment
return viewRoot;
}
}
JajananMenuFragment.java
package com.example.pesanpalgading20.ViewMenuHome;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.pesanpalgading20.R;
/**
* A simple {@link Fragment} subclass.
* Use the {@link JajananMenuFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class JajananMenuFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public JajananMenuFragment(String mParam1) {
this.mParam1 = mParam1;
}
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public JajananMenuFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment JajananMenuFragment.
*/
// TODO: Rename and change types and number of parameters
public static JajananMenuFragment newInstance(String param1, String param2) {
JajananMenuFragment fragment = new JajananMenuFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_jajanan_menu, container, false);
}
}
MinumanMenuFragment
package com.example.pesanpalgading20.ViewMenuHome;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.pesanpalgading20.R;
/**
* A simple {@link Fragment} subclass.
* Use the {@link MinumanMenuFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class MinumanMenuFragment extends Fragment {
public MinumanMenuFragment(String mParam1) {
this.mParam1 = mParam1;
}
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public MinumanMenuFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment MinumanMenu.
*/
// TODO: Rename and change types and number of parameters
public static MinumanMenuFragment newInstance(String param1, String param2) {
MinumanMenuFragment fragment = new MinumanMenuFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_minuman_menu, container, false);
}
}
so i found the solution in Tablayout does not display content
turn out the problem was the getFragmentmanager()
in my MenuFragment.java
when i changed to getChildFragmentManager()
there is no problem anymore