Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 524
NSouth Avatar asked Nov 24 '14 03:11

NSouth


People also ask

How do I block multiple instances of an application?

include the android:launchMode="singleTask" and it will not be possible to have multiple instances of your Activity launched at the same time.

How do I run an activity from another application?

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);

How can two different android applications interact explain in detail?

To take the user from one activity to another, your app must use an Intent to define your app's "intent" to do something. When you pass an Intent to the system with a method such as startActivity() , the system uses the Intent to identify and start the appropriate app component.


1 Answers

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.

like image 89
Shobhit Puri Avatar answered Sep 29 '22 18:09

Shobhit Puri