Kenny Tang
Kenny Tang

Reputation: 63

How may I prevent Cordova(3.2) App from being kill by Android in background?

Hi everyone this is my first question on StackOverflow.

I am now making an app with Cordova 3.2 with Telerik AppBulider and I am facing a problem on Android.

An app should not be kill when it was put in background. The app works fine if I put it in background and re-open it with the multitask menu. However if I re-open it with the app icon in the drawer, the app will be restart.

It only happens on Android (2.3, 4.4, didn't try other). No problem on iOS.

I have tried to Google it for solution and most of them bring me to this:

<preference name="KeepRunning" value="true"/>

which was documented in Cordova 3.2 Documentation, here my config.xml after adding it:

<?xml version="1.0" encoding="utf-8"?>
<cordova>
    <access origin="*"/>

    <content src="index.html" />

    <log level="DEBUG"/>
    <preference name="KeepRunning" value="true"/>

    <!-- For projects that target Apache Cordova 3.0.0 only, this <feature></feature> block ensures that button events and App plugin-related functionality will work as expected. -->
    <feature name="App">
        <param name="android-package" value="org.apache.cordova.App" />
    </feature>
</cordova>

But it doesn't help.

I have already listen to both pause and resume events.

document.addEventListener('deviceready', function() {
    document.addEventListener('pause', aFunction, false);
    document.addEventListener('resume', anotherFunction, false);
}, false);

How can I prevent the app from being restart when I re-open it with the app icon? Thanks :)

Upvotes: 4

Views: 5249

Answers (2)

Cocorico
Cocorico

Reputation: 2018

In my experience, "keepRunning" hasn't any effect in Android.

In Android, you can't avoid System to kill your app if it is in background, but you can make a service with the flag Start-Sticky. System will restart your service if it has been killed.

If you want to build complex apps, don't use Cordova but Native Android ! there's always many things you can't do in Cordova like work in background.

More Info :

follow this link : keepRunning PhoneGap/Cordova It is my question and I answer it with all info.

Upvotes: 2

mehsen
mehsen

Reputation: 588

Try adding adding

android:launchMode="singleInstance"

for activity tag in the AndroidManifest.xml , you can find it in projectName/platforms/android/AndroidManifest.xml.

This works for me :)

Upvotes: 2

Related Questions