Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap Android application restarting instead of resuming, although it was not killed by the OS

I have created an Phonegap 1.5/Android application.

My client reports that, when he leaves the app using the Home button, and then relaunches it using the app icon, the app relaunches from the start instead of resuming. However, when he holds the home button, the app appears in the recent apps, and when he accesses the app through this menu, the app resumes in the expected way.

I thought this could be linked to the app being automatically closed by the OS due to a lack of memory, but if that was the case the app shouldn't resume when clicked in the recent apps.

I could not reproduce the bug on my Sony Ericsson XPERIA with Android 2.3.4, the client has experienced this behaviour on a Motorola Defy and on another phone (i'll add the reference of the other phone and the OS versions as soon as I get them).

The initialization process of the app is declared this way :

window.addEventListener('load', function(){
      document.addEventListener('deviceready', _onDeviceReady, false);
}, false);

Could this be fixed by attaching the processes to other events (although I doubt it, the app really seems to be relaunched from the start) ?

Is there a declaration to make in the Android Manifest to prevent this behavior ?

Here is the activity signature in my AndroidManifest.xml

    <application android:debuggable="true" android:icon="@drawable/appicon"
    android:label="@string/app_name" >
    <activity android:configChanges="orientation|keyboardHidden" android:name=".MyAppActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 <activity android:name="com.phonegap.DroidGap" android:label="@string/app_name" 
           android:configChanges="orientation|keyboardHidden">
     <intent-filter>
     </intent-filter>
    </activity>
</application>

Is that a known bug in some Android phones/versions ?

Edit : holding the home button does not display the currently running apps but the recent apps. Why would the behavior in that menu be different from the main icon ?

like image 264
Lucas T Avatar asked Apr 12 '12 11:04

Lucas T


3 Answers

You want to add the following to the activity tag in your AndroidManifest.xml:

android:launchMode="singleTask"

So it should look like this:

<activity android:name="MyApp" android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
    android:launchMode="singleTask">

Check out this page for more information on launchMode: http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode

like image 72
Matt Bowman Avatar answered Nov 17 '22 16:11

Matt Bowman


Hey Lucas do you have:

android:configChanges="orientation|keyboardHidden"

in your activity tag in your AndroidManifest.xml.

As you said the other thing that could be happening is your client's phone is running low on memory and the OS is killing your app to free up memory.

like image 44
Simon MacDonald Avatar answered Nov 17 '22 15:11

Simon MacDonald


This is actually by design in Android, where the OS controls the life cycle of the application. You can find more information here.

like image 23
Waynn Lue Avatar answered Nov 17 '22 16:11

Waynn Lue