OnBackpressed get empty view on tablayout

橙三吉。 提交于 2019-12-12 11:13:34

问题


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

回答1:


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



回答2:


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

    }



回答3:


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;
    }



回答4:


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


来源:https://stackoverflow.com/questions/39268389/onbackpressed-get-empty-view-on-tablayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!