I'm trying to close/hide the ViewPager without also closing my Activity.

Here is a code from Android Developers website which unfortunately is not suitable for my needs because it closes the whole app:

public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            super.onBackPressed();
        } else {
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

1 Answers

0
Swapnil On

in onBackPressed() override, check if the view pager is visible. If it is, hide the view, or perform the desired action, else super.onBackPressed();

@Override
public void onBackPressed() {
    /* Suitable check to see if the view pager is visible to user */
    if (view_pager.getVisibility == View.VISIBLE) {
        view_pager.setVisibility(View.GONE);
    } else {
        super.onBackPressed();
    }
}