How to Bind Android Back Button to an Ion-slide?

All my slides are in the one index.html file and I want to achieve something like this:

if slideindex == 1 close app
else go to slideindex 1

Any ideas?

Hmm… No one? I must say, this forum is not very helpful… :frowning:

i do not understand what is your intention to close an app with sliding in a slidebox… the app users gonna hate you for sure.

Maybe you can post your current code in codepen.

And it is really annoying helping a person who wrote 2 posts (both in the same thread) and says that this forum is not helpful… -.-

My dear @bengtler this is not my only post on this forum…

Anyhow, I will repeat my request. I have a view that is made of a number of ion-slides. All slides are in that one view, in the index file. When I am on any slide and press the back button the app closes, but I only want it to go back to the first slide. And only if I am on slide 1 would the back button close the app. Is this more clear?

okay, this is default android backbutton behavior.

Therefore you have to overwrite your backbutton:

$ionicPlatform.registerBackButtonAction(function () {
  if (blabla) {
    // exitaApp
  } else {
    // go back with own back functionality
  }
}, 100);

now you can check, if your state === your start/slide state -> do nothing, else go back

That bit I already know, but how do you bind that to an ion-slide, basically the if bit is that’s causing issues. As in

if ($ionicSlideBoxDelegate.currentIndex() == 1){
exit app
} else {
go to $ionicSlideBoxDelegate.slide(2);
}

pseudocode obviously… :slight_smile:

But that is everything you must do?

exit the app with:
navigator.app.exitApp();

and set the correct slide is:
$ionicSlideBoxDelegate.slide(2);

1 Like
  $ionicPlatform.registerBackButtonAction(function () {
  if ($ionicSlideBoxDelegate.currentIndex() == 1) {
    navigator.app.exitApp();
  } else {
    $ionicSlideBoxDelegate.slide(2);
  }
}, 100);

I have done this, but now the back button is totally unresponsive. Btw, I really appreciate you trying to help.

what means “unresponsive” in that case?^^

That it does not do anything. Dot not close app, does not take you to desired slide. Does nothing. :frowning:

add console.logs to see, if you function gets called if you tap on back button.
check your javascript console for warnings if $ionicSlideBoxDelegate.currentIndex(), $ionicSlideBoxDelegate.slide() failed.

Then ionic can not find the slidebox

maybe you could put together a codepen or more code.

Yay! I made it work and here’s how.

function MainCtrl($scope, $ionicScrollDelegate, $ionicSlideBoxDelegate, $ionicPlatform) {
$scope.scrollTop = function() {
    $ionicScrollDelegate.scrollTop();
  };
  // controls slidebox buttons
  $scope.nextSlide = function() {
    $ionicSlideBoxDelegate.next();

  };
  $scope.previousSlide = function() {
    $ionicSlideBoxDelegate.previous();
  };
  $scope.s0Slide = function() {
    $ionicSlideBoxDelegate.slide(0);
  };
  $scope.s1Slide = function() {
    $ionicSlideBoxDelegate.slide(1);
  };
  $scope.s2Slide = function() {
    $ionicSlideBoxDelegate.slide(2);
  };

...
...
...

$scope.s43Slide = function() {
    $ionicSlideBoxDelegate.slide(43);
  };
  $scope.s44Slide = function() {
    $ionicSlideBoxDelegate.slide(44);
  };

  $ionicPlatform.registerBackButtonAction(function () {
  if ($ionicSlideBoxDelegate.currentIndex() == 1) {
    navigator.app.exitApp();
  } else if ($ionicSlideBoxDelegate.currentIndex() > 1 && $ionicSlideBoxDelegate.currentIndex() < 33){
      $ionicSlideBoxDelegate.slide(0);
  } else {
    $ionicSlideBoxDelegate.slide(2);
  }
}, 100);

Basically, once all the dependencies are in the controller you need and not in the .run everything is golden and working without issues. Thank you for your assist. :slight_smile:

i’m new in ionic, please tell me where i can put this code?