Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnBackpressed get empty view on tablayout

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:

this is my home fragment when I click on shirt it moves me to next fragment

As I said previous fragment moved me to this product detail fragment. and now when i press back button it gives me empty view

As I said it gives me this empty view

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();
like image 536
Chirayu Avatar asked Sep 01 '16 09:09

Chirayu


1 Answers

I had faced same issue when replacing fragment and that replaced fragment have TabLayout (i.e. Inner Fragments)

After researching about this issue: i found solution

What i does:

replacing

 ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());

to

If your Parent Activity is extending AppCompatActivity , then use

ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());

If your Parent Activity is extending Activity than use

ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getChildFragmentManager());
like image 99
ND1010_ Avatar answered Oct 04 '22 07:10

ND1010_