Use backstack with ViewPager

后端 未结 5 1452
栀梦
栀梦 2021-01-02 09:44

I am using a ViewPager to implement swiping in my android app. However, I would like the previous fragment to be shown when the user uses the back button instead of ending t

5条回答
  •  青春惊慌失措
    2021-01-02 10:03

    Overriding below method in Fragment Activity should solve your issue.

    @Override
    public void onBackPressed() {
        if (mViewPager.getCurrentItem() == 0) {
            // If the user is currently looking at the first step, allow the system to handle the
            // Back button. This calls finish() on this activity and pops the back stack.
            super.onBackPressed();
        } else {
            // Otherwise, select the previous step.
            mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
        }
    }
    

提交回复
热议问题