0

When pressing backbutton in android I want the app the be close and paused, not terminated, as when you press the homebutton. Is it any way to do this in cordova (ionic)? Like what method/event is "homebutton" firing?

Thanks

huadev
  • 433
  • 4
  • 15
  • You shouldn't try to do this. The behaviour of Back and Home buttons in Android are well-defined and understood by users - changing that behaviour is likely to cause annoyance. It might be better if you describe *why* you want the app paused instead of closed - there might be a better solution. – adelphus Aug 12 '15 at 17:18
  • You may be right. The scenario is when I am asking for a phonenumber verification by sms and the user presses backbutton which exits the app. The user then reads the sms code, relaunches my app and is shown the sign up screen. One solution I thought of is to use local storage, but this seemed much more complicated than just making backbutton behave as "close and pause app", as the home button does. The user will then be shown the verification screen when he/she reenters. – huadev Aug 14 '15 at 14:44
  • I would probably try and use [SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.html) to save the "awaiting verification" state and use it to show the correct screen when the app is launched. SharedPreferences is very simple key-value storage designed for this sort of thing. I'm not sure if ionic has support for it though. – adelphus Aug 15 '15 at 16:56

2 Answers2

4

May be its not too late. Actually I was also trying the same. I didn't get any solution. So I have created one plugin for it. Please refer this link Cordova Plugin - Back As Home

Add Plugin

cordova plugin add https://github.com/amitsinha559/cordova-plugin-back-as-home.git

Add this codes in .run if you want to work back as home button

$ionicPlatform.registerBackButtonAction(function(e){
    backAsHome.trigger(function(){
        console.log("Success");
    }, function(){
        console.log("Error");
    });
    e.preventDefault();
},101);

Note: Right now I don't have device. I have not tested the above code.

If you want to use in some button action

$scope.someButton = function() {
    backAsHome.trigger(function(){
        console.log("Success");
    }, function(){
        console.log("Error");
    });
}

Hope it will help :). Please let me know if you are facing any issue.

Sariban D'Cl
  • 2,197
  • 2
  • 28
  • 42
0
$ionicPlatform.registerBackButtonAction(function () {
  if ($state.current.name == "signIn"){
    navigator.app.exitApp();
  } else {
    navigator.app.backHistory();
  }
}, 100);

for reference please check the blogspot under register back button action and reply me if you have any queries

Anil kumar
  • 930
  • 7
  • 18