16

I am struggling to restrict my application to a single instance. Currently if the user presses home screen to quit the application, then does something outside and clicks on the application's icon again, it launches the App's second instance.

Here is my complete manifest file:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mydomain.qfa"
      android:versionCode="4"
      android:versionName="1.3">


<uses-sdk android:minSdkVersion="7"
          android:targetSdkVersion="13"
          android:maxSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application 
             android:debuggable=["false"]
             android:testOnly=["false"]
             android:icon="@drawable/icon.png"
>
<activity 
            android:name="com.mydomain.qfa"
            android:launchMode=["singleTask"]
            android:alwaysRetainTaskState="true"
            android:icon="@drawable/icon.png"
>
</activity>

</application>
</manifest>

Its a single activity app (basically no activities defined). On the main JQM page i have something like these entries:

<div data-role="page" id="HomePage">

    <div data-theme="d" data-role="header" data-position="fixed" style="padding-bottom: 0px;" data-tap-toggle="false">

        <div data-role="navbar">                                      

    <div data-role="content"  class="MainContent"  style="overflow:hidden; padding-top: 0px;">

Can someone please tell me if my Manifest is correct and if I should be using

android:name="com.mydomain.qfa"

or should it something else like

android:name="com.mydomain.qfa.HomePage"?

or

android:name="com.mydomain.qfa.MainContent"?

Thanks in Advance.

AnR
  • 1,809
  • 3
  • 26
  • 45

4 Answers4

42

I struggled with this problem for 2 days. The fix for adding this attribute was only recently added to Cordova as of 3.5, issue CB-6048

add:

<preference name="AndroidLaunchMode" value="singleTask" />

to config.xml

Other available values:

  • "standard"
  • "singleTop"
  • "singleTask"
  • "singleInstance"
kyler
  • 633
  • 5
  • 10
  • 1
    This looks like the perfect solution for a problem I've got, but it doesn't update my Android Manifest.xml. I've added the `android:launchMode="singleTop"` manually and run the project, all is fine, but running `cordova build android` with `` removes the actual `launchMode` tag from the XML file – Pete Jul 24 '14 at 08:36
  • I too thought my `AndroidManifest.xml` wasn't updated, but it turned out that my `AndroidLaunchMode` was appended to the end of the file (while the default entry appeared at the top of the file). Although there are now two entries in the file, it appears that the last entry wins. – sherb Oct 08 '14 at 00:40
2

Try with

<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:launchMode="singleTop" android:name="HomePage" android:theme="@android:style/Theme.DeviceDefault">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

it's what's generated by phonegap CLI and launchmode set to singleTop to have single instance.

android:name must match the name of the main java class of the activity, not the full package name. By default phonegap sets it as the app name.

QuickFix
  • 11,661
  • 2
  • 38
  • 50
0

This is what ultimately worked for me:

However the trick is that you need to change in two locations:

A) MyAppFolder\platforms\android\AndroidManifest.xml

<activity android:alwaysRetainTaskState="true" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:launchMode="singleTask" android:name="AppName" android:theme="@android:style/Theme.Black.NoTitleBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

B) MyAppFolder\www\AndroidManifest.xml

<activity
            android:name="AppName"
            android:launchMode=["singleTask"]
            android:alwaysRetainTaskState="true"
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

Hope that helps someone.

AnR
  • 1,809
  • 3
  • 26
  • 45
  • Really? Will check after the next release. Can't afford to experiment at the last moment. Thanks for your point anyway. – AnR Feb 13 '14 at 17:21
  • Facing the same issue... However, I also don't see a www/androidmanifest.xml in my phonegap app. – ericpeters0n May 25 '14 at 04:05
  • @Anjum can you double check this please? – ericpeters0n May 25 '14 at 04:17
  • My understanding is that AndroidManifest.xml is created by default when you create a PhoneGap project. But if that is no present just create one. Solution below by @QuickFix worked for me. – AnR May 26 '14 at 05:22
0

try this:

<gap:config-file platform="android" parent="/manifest/application">
    <activity android:launchMode="singleInstance" />
</gap:config-file>

but in your config's widget setting, it should be like this (add the android namespace definition):

<widget xmlns     = "http://www.w3.org/ns/widgets"
        xmlns:gap = "http://phonegap.com/ns/1.0"
        xmlns:android = "http://schemas.android.com/apk/res/android"
        id        = "com.wildabeast.app"
        version   = "1.0.0">
....
</widget>
Bit_Pulse
  • 336
  • 1
  • 2
  • 16