我有一个片段重置并显示空白页而不是视图

时间:2016-10-19 10:16:58

标签: android android-fragments

我有一个侧抽屉,用于在我的不同片段之间导航,找到下面的代码。

 public boolean onNavigationItemSelected(MenuItem item)
    {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_dash)
        {
            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
            xfragmentTransaction.replace(R.id.containerView,new dashFragment()).commit();
        }
        else if (id == R.id.nav_profile)
        {
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.containerView,new profileFragment()).commit();
        }
        else if (id == R.id.nav_notify)
        {
            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
            xfragmentTransaction.replace(R.id.containerView,new notiFragments()).commit();
        }
        else if (id == R.id.nav_contacts)
        {
            FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
            xfragmentTransaction.replace(R.id.containerView,new contactsFragment()).commit();
        }
        else if (id == R.id.signout)
        {
            startActivity(new Intent(this,loginActivity.class));
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

我遇到的问题是,当我离开初始片段(即dashFragment)并访问另一个片段(如profileFragment并返回dashFragment)时,dashFragment会显示一个空白页面而不是显示我的按钮和其他项目。以下是我的dashFragment的代码。

public class dashFragment extends Fragment

{
    public static TabLayout tabLayout;
    public static ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private List<String> titleList = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        View x = inflater.inflate(R.layout.tablayout, null);

        tabLayout = (TabLayout) x.findViewById(R.id.tabs);
        viewPager = (ViewPager) x.findViewById(R.id.viewpager);

        fragmentList = new ArrayList<>();

        prepareDataResource();
        titleList();

        topLevel rta = new topLevel(getFragmentManager(),fragmentList,titleList);
        viewPager.setAdapter(rta);
        viewPager.setOffscreenPageLimit(10);
        tabLayout.setupWithViewPager(viewPager);

        setTabIcons();

        return x;

    }

    private void prepareDataResource()
    {
        fragmentList.add(new homeFragment());

        fragmentList.add(new resultsFragment());
    }

    private void titleList()
    {
        titleList.add("Home");
        titleList.add("Results");
    }

    private void setTabIcons()
    {

        tabLayout.getTabAt(0).setIcon(R.drawable.home_tab);
        tabLayout.getTabAt(1).setIcon(R.drawable.results_tab);
    }

}

1 个答案:

答案 0 :(得分:0)

您需要添加addToBackStack()方法。

FragmentManager fragmentManager = getSupportFragmentManager();                 fragmentManager.beginTransaction()。replace(R.id.root_view,new TimeChanging())。 addToBackStack (new TimeChanging()。getClass()。getName())。commit();

当您加载第一个片段时,不要替换为HOME添加片段或默认。

要清除Back Stack,请使用:

public void clearBackStack(){

    FragmentManager manager = getSupportFragmentManager();
    Log.i("Fragment Count", String.valueOf(manager.getBackStackEntryCount()));
    if (manager.getBackStackEntryCount() > 0) {
        FragmentManager.BackStackEntry first = manager.getBackStackEntryAt(0);
        manager.popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
}