我正在使用viewpager在片段之间滑动,并希望后退按钮导航到先前查看的片段而不是结束活动.很抱歉,如果这是
this question的副本,但我没有找到答案非常有帮助.显然onBackPressed需要被覆盖,但我不知道如何获取并显示正确的片段.我假设我应该使用fragmentmanager的backstack,但是getSupportFragmentManager().getBackStackEntryCount()总是返回0.我是否需要使用FragmentTransaction.addToBackStack()手动将片段添加到backstack?如果是这样,我会在适配器中添加它?
这是我活动的代码,
public class PagerActivity extends FragmentActivity { ArrayList<Sale> sales; MyAdapter mAdapter; ViewPager mPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent it = getIntent(); this.sales = (ArrayList<Sale>) it.getExtras().get("sales"); int position = it.getExtras().getInt("position"); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); setContentView(R.layout.fragment_pager); mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager) findViewById(R.id.pager); mPager.setAdapter(mAdapter); mPager.setCurrentItem(position); } public class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fragmentManager) { super(fragmentManager); } @Override public int getCount() { return sales.size(); } @Override public void destroyItem(ViewGroup container,int position,Object object) { super.destroyItem(container,position,object); } @Override public Fragment getItem(int position) { SalesThumbFragment frag = new SalesThumbFragment(); return frag.newInstance(sales.get(position)); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.sales_controller,menu); return true; } @Override public void onBackPressed() { if(getSupportFragmentManager().getBackStackEntryCount() != 0) { getSupportFragmentManager().popBackStack(); } else { super.onBackPressed(); } } }
解决方法
在新设计支持库中,我使用它
我有同样的问题,我按照这一步
我有同样的问题,我按照这一步
在viewpager中有3个片段的主要活动中,我创建堆栈
和推送和弹出数据.
//private Stack<Integer> stackkk; ==> As i get edit suggestion private Stack<Integer> stackkk = new Stack<>(); // Edited private ViewPager mPager; private int tabPosition = 0; mTabLayout.setupWithViewPager(mPager); mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout)); mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { tabPosition = tab.getPosition(); mPager.setCurrentItem(tab.getPosition()); if (stackkk.empty()) stackkk.push(0); if (stackkk.contains(tabPosition)) { stackkk.remove(stackkk.indexOf(tabPosition)); stackkk.push(tabPosition); } else { stackkk.push(tabPosition); } } @Override public void onTabUnselected(TabLayout.Tab tab) { tabPositionUnselected = tab.getPosition(); } @Override public void onTabReselected(TabLayout.Tab tab) { } }); }
并且在onBackPressed活动中,
@Override public void onBackPressed() { if (stackkk.size() > 1) { stackkk.pop(); mPager.setCurrentItem(stackkk.lastElement()); } else { } }