15

I have a problem with my phonegap application. I want to minimize the application (send App in background so that it's still running) when back button is pressed.

Here's my code.

 document.addEventListener("backbutton", onBackKeyDown, false);
 function onBackKeyDown() {
 }

If I use navigator.app.exitApp(); - my application will terminate and If I use navigator.app.backhistory() - It will just go back to the previous page.

I want that if I press Back button, it will send me to Home screen and send the application to background so that it's still running. Thank you.

Serge P
  • 1,173
  • 3
  • 25
  • 39
miles
  • 225
  • 3
  • 10
  • 20

2 Answers2

14

you can use this plugin then when device ready listen to backbutton and when onBackKeyDown use the plugin to launch the home screen

`document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
    navigator.Backbutton.goHome(function() {
        console.log('success')
    }, function() {
        console.log('fail')
    });
}
`

this plugin is just updated from Dpa99c answer for help user with cordova >=3

Community
  • 1
  • 1
Mohamed Salah
  • 1,506
  • 1
  • 14
  • 23
  • which version you use? – Mohamed Salah Sep 14 '14 at 07:34
  • Great, it works with CLI 4.1, but is there any chance of making this plugin compatible with Windows Phone 8 as well? That'd be very much appreciated, thanks. – andreszs Feb 13 '15 at 13:17
  • Thanks a lot, using the referenced plugin and writing the override on `backbutton` event solved the issue – Farzad Yousefzadeh Jun 24 '16 at 14:23
  • I can't believe you actually need to install a plugin to do this. Why is Cordova so crippled that it does not even provide the most essential functions? Do they have something against Cordova developers?? – andreszs Jun 11 '17 at 16:29
6

Your best bet is to use a plugin to launch the home screen. I've already created one for my own purposes - you can download my Eclipse test project containing the source code and resulting compiled APK from here.

You would then use my plugin to override the default behaviour of the back button something like this:

function onBackKeyDown(e) {
  e.preventDefault();
  cordova.require('cordova/plugin/home').goHome(function(){
    console.info("Successfully launched home intent");
  }, function(){
    console.error("Error launching home intent");
  });  
}
document.addEventListener("backbutton", onBackKeyDown, false);

Here's the plugin source code:

Home.java

package org.apache.cordova.plugin;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.util.Log;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;

public class Home extends CordovaPlugin {

    private static final String LOG_TAG = "HomePlugin";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if ("goHome".equals(action)) {
            try {               
                Intent i = new Intent(Intent.ACTION_MAIN);
                i.addCategory(Intent.CATEGORY_HOME);
                this.cordova.getActivity().startActivity(i);

            } catch (Exception e) {
                Log.e(LOG_TAG, "Exception occurred: ".concat(e.getMessage()));
                return false;
            }
            callbackContext.success();
            return true;
        }
        Log.e(LOG_TAG, "Called invalid action: "+action);
        return false;  
    }
}

home.js

cordova.define("cordova/plugin/home", function(require, exports, module) {
    var exec = require('cordova/exec');
    var Home = function() {};
    Home.prototype.goHome = function(successCallback, errorCallback) {
        return cordova.exec(successCallback, errorCallback, 'Home', 'goHome', []);
    };
    var home = new Home();
    module.exports = home;
});

config.xml

<plugin name="Home" value="org.apache.cordova.plugin.Home"/>
DaveAlden
  • 30,083
  • 11
  • 93
  • 155