4

I have my activity in AndroidManifest.xml :

    <activity android:name="mobile_app" >
    </activity>

I want to add an attribute to the activity to be like this:

    <activity android:name="mobile_app" android:launchMode="singleInstance" >
    </activity>

I know that I can add the attribute directly in the androidManifest.xml an it works but I want my plugin to add the attribute to the activity tag.

Any help please ?

mehsen
  • 588
  • 2
  • 7
  • 18

3 Answers3

2

I too need to do this, but it looks like it's not possible:

The config-file element only allows you to append new children to an XML document tree.

https://cordova.apache.org/docs/en/5.0.0/plugin_ref_spec.md.html

Community
  • 1
  • 1
cloakedninjas
  • 4,007
  • 2
  • 31
  • 45
1

It looks like hooks are the way to do it. I did this in a way similar to that suggested in https://stackoverflow.com/a/32394342/2569560

In config.xml, inside <platform name="android"> , add

<hook type="after_build" src="scripts/androidMainActivityAttributeAdd.js" />

Then, add a script called androidMainActivityAttributeAdd.js. Here you append the attribute inside the activity tag .

#!/usr/bin/env node

module.exports = function(context) {

  var fs = context.requireCordovaModule('fs'),
    path = context.requireCordovaModule('path');

  var platformRoot = path.join(context.opts.projectRoot, 'platforms/android');


  var manifestFile = path.join(platformRoot, 'AndroidManifest.xml');

  if (fs.existsSync(manifestFile)) {

    fs.readFile(manifestFile, 'utf8', function (err,data) {
      if (err) {
        throw new Error('Unable to find AndroidManifest.xml: ' + err);
      }

      var attribute = 'android:launchMode="singleInstance"';

      if (data.indexOf(attribute) == -1) {

        var result = data.replace(/android:name="MainActivity"/g, 'android:name="MainActivity" ' + attribute);

        fs.writeFile(manifestFile, result, 'utf8', function (err) {
          if (err) throw new Error('Unable to write into AndroidManifest.xml: ' + err);
        })
      }
    });
  }


};
Community
  • 1
  • 1
0

Add this to your plugin.xml for your android platform element:

<platform name="android">
    <config-file target="AndroidManifest.xml" parent="/manifest/application">
        <activity android:name="mobile_app" android:launchMode="singleInstance" />
    </config-file>
</platform>
Lee Crossley
  • 1,280
  • 12
  • 17
  • I tried this but it is appending a new activity tag to the application tag. So I am having 2 activities with the same android:name is that fine ? – mehsen Aug 06 '14 at 11:20
  • The GoogleGlass plugin does it like this: https://github.com/aphex/cordova-glass-core/blob/5e160a432bb20b37f4671a5f6687ae32df5b0406/plugin.xml#L41 – Lee Crossley Aug 06 '14 at 11:24
  • I think GoogleGlass plugin is adding a new activity to the application but what I want is to update an existing activity – mehsen Aug 06 '14 at 11:36
  • Yes, it is adding a new activity, how would you modify an existing activity when the android:name is dynamic? or would you want to just update all to add the android:launchMode attribute? – Lee Crossley Aug 06 '14 at 11:40
  • I want to update the activity with the name `android:name="mobileapp"` so I add the `android:launchMode="singleInstance"` – mehsen Aug 06 '14 at 11:43
  • Sorry, I think you'll have to do this manually / via a hook. I'm not sure a plugin should do this, perhaps check your implementation. – Lee Crossley Aug 06 '14 at 11:48