Hey guys please look into the matter. I am stuck in solving this problem.
I have set tabs in fragment using TabLayout
and ViewPager
. The problem is when i move to next fragment and then i press back button i get empty tabs view.
Here I am attaching my code.
home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="4dp" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>`
home.java(fragment)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home, container, false);
((Drawer) getActivity()).setActionBarTitle("Home");
Toast.makeText(getActivity(), "entry count" + getActivity().getSupportFragmentManager().getBackStackEntryCount(), Toast.LENGTH_SHORT).show();
tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Products"));
tabLayout.addTab(tabLayout.newTab().setText("Category"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) view.findViewById(R.id.pager);
Pager adapter = new Pager(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
tabLayout.getTabCount());
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(this);
return view;
}
@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) {
}
pager.java (Adapter)
public class Pager extends FragmentStatePagerAdapter {
int tabCount;
public Pager(FragmentManager fm, int tabCount) {
super(fm);
this.tabCount = tabCount;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Product tab1 = new Product();
return tab1;
case 1:
Category tab2 = new Category();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
return tabCount;
}
}
MainActivity (where I set this tab fragment)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
getSupportActionBar().setTitle("Home");
fragment = new home();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main, fragment);
ft.commit();
You do not need to attach
PageListeners
totabLayout
andtabListeners
toViewPager
.This will take care of this.
Your updated code can be like this. Do tell me if you still face any problem.
write you code in activity
I had faced same issue when replacing
fragment
and that replacedfragment
haveTabLayout
(i.e.Inner Fragments
)After researching about this issue: i found solution
What i does:
replacing
to
If your Parent
Activity
is extendingAppCompatActivity
, then useIf your Parent Activity is extending
Activity
than useYou should not create an
Adapter
inside a fragment with its activity'sFragmentManager
. Try to replacewith