OnBackpressed get empty view on tablayout
Asked Answered
M

4

9

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();
Multifaceted answered 1/9, 2016 at 9:57 Comment(10)
this is not valid in viewpager because when you will swipe left or right it will comfusing to which fragment is last visited so avoid this type of senarios !!Loiret
do not add fragments of viewpager in backstackLoiret
So what changes should i made ? @VishalPatoliyaMultifaceted
@Chirayu: how do you move to next fragment? by swiping or clicking tab?Unwell
viewpager is in activity or in fragment ?Loiret
@VishalPatoliya view pager is in fragment as i have described above and i use getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.main, fragment).addToBackStack(null).commit(); to move to next fragment that is in my product fragment(one of tab fragment)Multifaceted
when you replace this fragment from activity put that activity codeLoiret
or it is default fragement of activity?Loiret
@Unwell i use to move in next fragment using getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.main, fragment).addToBackStack(null).commit(); that is into my tab's fragmentMultifaceted
Hi, Did you sole this issue?Estell
T
16

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());
Talcahuano answered 7/12, 2017 at 9:49 Comment(0)
E
0

write you code in activity

 @Override
    public void onBackPressed() {

            try {
                if (getFragmentManager().getBackStackEntryCount() > 1) {
                    FragmentManager.BackStackEntry first = getFragmentManager().getBackStackEntryAt(0);
                    getFragmentManager().popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    onNavigationItemSelected(navigationView.getMenu().getItem(0));
                } else
                    finish();

            } catch (Exception e) {
                e.printStackTrace();
            }

    }
Enclosure answered 1/9, 2016 at 10:14 Comment(1)
Comments are not for extended discussion; this conversation has been moved to chat.Santonin
U
0

You do not need to attach PageListeners to tabLayout and tabListeners to ViewPager.

This will take care of this.

tabLayout.setupWithViewPager(viewPager);

Your updated code can be like this. Do tell me if you still face any problem.

        @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());

 //Link tabLayout with ViewPager.
        tabLayout.setupWithViewPager(viewPager);

     return view;
    }
Unwell answered 1/9, 2016 at 10:15 Comment(3)
it is not previewing my tabs and its following fragments.Multifaceted
@Chirayu: edited my answer, tabLayout.setupWithViewPager(viewPager); should be called at last before return. can you please try and let me know?Unwell
oh yes i have noticed this and i have tried implemented it like this onlyMultifaceted
B
0

You should not create an Adapter inside a fragment with its activity's FragmentManager. Try to replace

Pager adapter = new Pager(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());

with

Pager adapter = new Pager(getChildFragmentManager(), tabLayout.getTabCount());
Burk answered 6/12, 2016 at 7:24 Comment(1)
appreciate your time. i have tried it but it is not working. :(Multifaceted

© 2022 - 2024 — McMap. All rights reserved.