Would it be possible to pause an Android PhoneGap application manually? I need to pause the application and go to background when somebody click a button. I used navigator.app.exitApp();
but it completely closes the application. I do not want to close the application just unload just like is done with the native back button. Please help, thanks.
Asked
Active
Viewed 3,685 times
5

Perception
- 79,279
- 19
- 185
- 195

Sarath DR
- 551
- 5
- 11
-
What do you mean with pause? Maybe have a look at the Activites Lifecircle cause I'm not sure what you try to achieve :) http://developer.android.com/guide/topics/fundamentals/activities.html – nuala May 11 '12 at 10:20
-
I need to pause/stop the activity( The app should not be visible in forground) and app should not be visible like when we click native back button, it will hide the app. – Sarath DR May 11 '12 at 10:49
-
1Basically you want the same behaviour as if someone pressed the Home button correct? – Simon MacDonald May 11 '12 at 13:54
-
Yes Simon, Any chance ?? – Sarath DR May 14 '12 at 09:23
-
Has anyone came up with a solution for this? Is there a function within Phonegap to do just this?? – Chris Oct 02 '12 at 13:59
-
I'm also looking for a solution. – mikrobi Sep 24 '13 at 09:12
2 Answers
3
Here's a solution which is similar to Joram Teusink's answer, but easier because you don't need to manually edit java code - just install a plugin with the phonegap / cordova CLI.
The plugin one function: goHome(). If you call this, the app will pause - just like pressing the home button.
Usage:
navigator.Backbutton.goHome(function() {
console.log('success - the app will now pause')
}, function() {
console.log('fail')
});
Installation:
phonegap local plugin add https://github.com/mohamed-salah/phonegap-backbutton-plugin.git
Here's the github page:
https://github.com/mohamed-salah/phonegap-backbutton-plugin.git

Lucidity
- 1,299
- 17
- 19
1
In your Javascript:
// HomeButton
cordova.define("cordova/plugin/homebutton", function (require, exports, module) {
var exec = require("cordova/exec");
module.exports = {
show: function (win, fail) {
exec(win, fail, "HomeButton", "show", []);
}
};
});
And:
// HomeButton
function homeButton() {
var home = cordova.require("cordova/plugin/homebutton");
home.show(
function () {
console.info("PhoneGap Plugin: HomeButton: callback success");
},
function () {
console.error("PhoneGap Plugin: HomeButton: callback error");
}
);
}
In Java on Android Native:
package org.apache.cordova.plugins;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import android.content.Intent;
import android.util.Log;
public class HomeButton extends CordovaPlugin {
public static final String LOG_PROV = "PhoneGapLog";
public static final String LOG_NAME = "HomeButton Plugin";
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
Log.d(LOG_PROV, LOG_NAME + ": Simulated home button.");
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.cordova.startActivityForResult(this, i, 0);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
return true;
}
}
Call it with:
homeButton();
It works, and is part of a repo of mine: https://github.com/teusinkorg/jpHolo/

Eliran Malka
- 15,821
- 6
- 77
- 100