Problems with the return button in native ionic

I have a problem with the back button from the Android, my project is returning only once in the history of the application. I am using the ionic version 1.0.0-beta.14. My Code is the following:

  .run(function($ionicPlatform,$ionicPopup, $state,$ionicHistory){
    $ionicPlatform.registerBackButtonAction(function (event) {
        alert("entro aqui");;
      else {
    }, 100);

I am also with the following problem, when in pagina home, instead of returning in the historia i need him to quit the application. I am trying to and looking for this in several places, and up to the present moment nobody has managed to assist me in this. I am rookie with ionic and i would be grateful for a help. Thank You`