Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:39:05+00:00 2026-06-13T18:39:05+00:00

I am having two fragments Frag A and Frag B which are rendered using

  • 0

I am having two fragments Frag A and Frag B which are rendered using view pager .
If user has swiped from A to B then presses back button(when in B) then user should go to A instead of coming out of view pager . How can we achieve this ?

Adding transaction to backstack does not seem to help .

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:39:06+00:00Added an answer on June 13, 2026 at 6:39 pm

    You have to override the onKeyDown() method in the Activity.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            myViewPager.setCurrentItem(0, true);
            return true;
        } else {
           return super.onKeyDown(keyCode, event);
        }
    }
    

    This will capture the “back” button press event and send the user to the first item in the ViewPager.

    There is also ViewPager#getCurrentItem() which can be used to go back only if the user swiped. An implementation like this:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && myViewPager.getCurrentItem() == 1) {
            myViewPager.setCurrentItem(0, true);
            return true;
        } else {
           return super.onKeyDown(keyCode, event);
        }
    }
    

    would send the user back to the previous page only if the user was on the next page. Else, it will register the “back” normally and the user will exit as expected.

    You may want the case in which the user goes back to the previous page (assuming you have more than two pages). In which case, you want to implement the ViewPager.OnPageChangeListener() and save the position every time onPageSelected(int position) is called. If you implement them in a stack, then you can pop them off every time the user presses “back”. Once the stack is empty, exit the app.

    EDIT

    As correctly stated in the comments by Jade Byfield, a slightly easier way to do this would be to use onBackPressed() in the Activity. This will only work in API levels 5+.

    @Override
    public void onBackPressed() {
      if(i_dont_want_to_leave) {
         myViewPager.setCurrentItem(0, true);
      } else {
        super.onBackPressed(); // This will pop the Activity from the stack.
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on an Android tablet application which has two fragments, a
I am having two issues: I usually create separate view for the login to
Having two lists [A,B,C,D] and [1,2,3,4] , I want to provide a user interface
I am having two entity files one as user.php and another as usertype.php. now
I have two tabs using tabhost. One tab having activity extends ExpandableListActivity. It works
Having two lists of same object type. I want to join them using an
In my application i am having a TabActivity in which i am using One
I'm having two sortable lists where I can add items from the first list
I am having two problems regarding compiling and running an Eclipse java project from
I am having two problems. First: Trying to submit this form using jquery. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.