android - How to handle back button using view pager? -


I have two pieces of Frag A and Frag B, which are provided using View Pager. If the user is in A, then the back button is pressed (when in B), then the user should go to A rather than exit the pager. How can we get it?

Backstack does not help in adding transactions.

Thanks

You must override onKeyDown () Method Activity .

  @Override Public Boolean ON KIDDOWN (Int kikod, key event event) {if (keycode == KeyEvent.KEYCODE_BACK) {myViewPager.setCurrentItem (0, true); Back true; } Else {return super.onKeyDown (keyCode, event); }}   

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

There can also be used to return only if the user swiped an implementation like this:

  @KeyDown Public Boolean override (int kikod, key event event ) {If (keycode == KeyEvent.KEYCODE_BACK & myViewPager.getCurrentItem == 1} {myViewPager. SetCurrentItem (0, true); Back true; } Else {return super.onKeyDown (keyCode, event); }}   

The user must send back to the previous page, if the user was on the next page. Otherwise, it will normally register "back" and the user will exit expectantly

You may want the case in which the user goes back to the previous page (assuming you have more than two Pages). In that case, you want to implement and always call onPageSelected (int position) . If you implement them in the stack, you can pop them every time the user clicks "back", once the heap is empty, exit the app.

Edit

In comments by Jade Byfield correctly said, an easy way to do this is to onBackpressed () < / Code> will only work in API level 5+.

  Override public zeros at @BackPressed (if i_dont_want_to_leave) {myViewPager.setCurrentItem (0, true);} and {super.backpress (); // it pops up activity from stack Will}.}}    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -