How to handle multiple instances of an application on access with a different intent

I just added the ability for a user to restore their data in my application by backing up a custom file type. When a file is opened from the file explorer with my application, my main (active) activity is opened, but in a new instance. For example, if my application is open and the recovery file is opened from the file explorer, a new instance of my application opens when I take an active action. In Android Task Manager, this is similar to my application running in file explorer, if that makes sense. Since the recovery process modifies user data, it is possible that the original instance of my application stops functioning, because it is in a data-dependent activity.

How can I prevent multiple instances of my application when it is accessed through a different intent? Thanks.

Update: I searched and still cannot find a solution. Help will be appreciated.

filter intent for my root activity in 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> 
+5
source share
1 answer

How can I prevent multiple instances of my application when it is accessed through a different intent?

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

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

This probably will not create a new instance, as you mentioned in the question. Give it a try. Let me know if you are still facing the same problem.

+3
source

Source: https://habr.com/ru/post/1207552/


All Articles