NSouth
NSouth

Reputation: 5276

How to handle multiple instances of app when accessed via a different intent

I've just added the ability for a user to restore their data in my app by backing up a custom file type. When the file is opened from a file explorer with my app, my root (main) activity is opened, but in a new instance. For example, if my app is open and the restore file is opened from a file explorer, a new instance of my app opens at my root activity. In android's task manager, this looks like my app running within the file explorer, if that makes sense. Since the restore process changes user data, it's possible that the original instance of my app stops functioning because it was in a data-dependent activity.

How can I prevent multiple instances of my app when it is accessed via a different intent like this? Thanks.

Update: I've searched around and still can't find a solution. Help would be appreciated.

intent-filter for my root activity in the manifest:

<manifest
    android:launchMode="singleInstance"
...
<application
    android:... >
<intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="file" android:pathPattern=".*\\.grdcsv" android:mimeType="*/*" android:host="*"/>
</intent-filter>

<!-- For email attachments -->
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="application/octet-stream" android:pathPattern=".*\\.grdcsv" android:scheme="content" />
</intent-filter>

Upvotes: 8

Views: 3253

Answers (2)

SingleTask did not work for me. However, this worked:

android:launchMode="singleInstance"

Upvotes: 0

Shobhit Puri
Shobhit Puri

Reputation: 26007

How can I prevent multiple instances of my app when it is accessed via a different intent like this?

You can try to use android:launchMode="singleTask" in your activity declaration in manifest. If you see the docs, it says:

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

This probably won't create a new instance as you mentioned in the question. Try it. Let me know if you still face the same issue.

Upvotes: 6

Related Questions