Android back button support

Android phones have a separate back button that will undo the previous action. Currently only navigation is handled (automatically I believe), but if that previous action was to open a modal then I think that modal should close on back. I would also extend the same functionality to action sheets, side menus and list options too. If they are open then close them when back is pressed.

Or should Ionic leave handling of the Android back button to the developer? (Personally I think the framework should handle it)

No I agree, the framework should do the handling of Android’s back button. I’m actively working on a branch that will handle all of this for you. To be continued…

2 Likes

Really interested to see what you come up with Adam.

Any news on this @adam? The app now crashes on Android when pressing the back button on the first screen…

@coen_warmer what version are you using? We’ll try to replicate this on our end and come up with a fix. Thanks for letting us know. /cc @tim

@adam Had this on 0.9.18 with the router based navigation. I’ve since updated my project to 0.9.20 so will retry and report back.

Hey Adam,

would be possible to disable somehow that back button? For example, after login, I would like to disable going back to login, or maybe detect and do automatic logout or something like that…

edit: I found similar question already asked Is there a way to clear out the nav-router history and prevent a back button?, reseting stack of states will also do the job as there will not be 'where to go back… I guess :slight_smile:

thanks

There is a $ionicViewService.clearHistory() that I created for another reason, but maybe this would do the trick your case? Would you be able test and see if it works for you in your case?

cc: @Calendee

It seems promising :slight_smile: will try it, thanks

I also like to disable the back button on some pages. I tried using the $ionicViewService.clearHistory(), but now that there is no history, hitting the android back button closes the app. Diggin into the code I see that $ionicPlatform has some events to attach and detach events to the android back button, but i can’t make them work. Any other ideas?

1 Like

Hello, is there a way to close the app when user clicks on android back button while on certain screen?
for example, an app has 3 views:

-- login
  -- main
    -- search 

Once user is logged in I want to prevent prevent navigation to login page by using back button.
Thoughts?
Thanks

You can prevent this by either clearing the navigation when the user enters the main screen. Or either add a listener on the main screen and exit the app when the backbutton is pressed.

Any news from this feature that would allow attaching actions to the back button event ?

you can always try something like :

  $ionicPlatform.registerBackButtonAction(function (event) {
    if($ionicHistory.currentStateName() == "myiew"){
      ionic.Platform.exitApp();
      // or do nothing
    }
    else {
      $ionicHistory.goBack();
    }
  }, 100);

remember you have to use this inside the .run