如何在选项卡上实现Backstack?

时间:2019-05-31 15:59:46

标签: android android-fragments tabs

我正在使用正常工作的标签布局,我的问题是,当我向后按设备离开应用程序而不是回到上一个片段时,有可能使应用程序返回到上一个片段碎片?

  

onCreate MainActivity

tabLayout = findViewById(R.id.tab_menu);
        viewPager = findViewById(R.id.main_content);

         MainFragmentAdapter adapter = new 
         MainFragmentAdapter((getSupportFragmentManager()));
        adapter.AddFragment(new Fragment1(), "1");
        adapter.AddFragment(new Fragment2(), "2");
        adapter.AddFragment(new Fragment3(), "3");
        adapter.AddFragment(new Fragment4(), "4");

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

我的片段适配器

public class MainFragmentAdapter extends FragmentPagerAdapter {

    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> FragmentListTitles = new ArrayList<>();

    public MainFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return FragmentListTitles.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return FragmentListTitles.get(position);
    }

    public void AddFragment(Fragment fragment, String Title) {
        fragmentList.add(fragment);
        FragmentListTitles.add(Title);
    }
}

0 个答案:

没有答案