I have a simple app with a single TextView which displays plain text from an ACTION_SEND intent. My issue is that every time that some text is shared to this app a new instance is created. I can see multiple instances of the app on checking the recent apps. I'm testing it on API 23.
This is my code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity.java", "onCreate");
((TextView) findViewById(R.id.temp_textview)).setText("Share text/links from other apps");
}
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
String action = intent.getAction();
intent.getFlags();
Log.d("onResume - intent: ",intent.toString());
String type = intent.getType();
TextView displayText = (TextView) findViewById(R.id.temp_textview);
if (Intent.ACTION_SEND.equals(action) && type!=null) {
Log.d("MainActivity.java", "Intent verified");
if ("text/plain".equals(type)) {
handleSendText(intent, displayText);
}
}
}
void handleSendText(Intent intent, TextView displayText) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
Log.d("MainActivity.java", sharedText);
if (sharedText != null) {
displayText.setText(sharedText);
}
}
}
I have tried fiddling with the launchMode in the manifest but none of the options solved the issue.
Edit 1:
Here's my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="chaitanya.im.example">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<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.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
</application>
</manifest>
You can create a filter that includes more than one instance of <action> , <data> , or <category> . If you do, you need to be certain that the component can handle any and all combinations of those filter elements.
An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.
Generally speaking, apps that only run a single instance do so based on their nature. Some apps do not need to run multiple instances. Other apps such as browsers, video players, word processors, etc., need to run multiple instances, and they do. That said, if you want to limit an app from running multiple instances, you can.
In order to disable multiple instances of an app on Windows 10, you need to install a free app called SingleInstance. Go ahead and download, and run the app. The app, by default, has one app pre-configured and that’s the Calculator app on Windows 10.
However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent () call); a new instance is not created.
It seems that launching it from the Play Store uses a different Intent than launching it from the phone's application menu of icons. This is leading to multiple copies of the same Activity being launched, which are conflicting with each other.
Normally, if another application launches your Activity
(via ACTION_SEND, for example), your Activity
will be launched into the existing task of the other app. So if you use 5 other apps and each of them launch your Activity
using ACTION_SEND, you will have 5 instances of your Activity
, each in a separate task.
If you want your Activity
to run by itself, in its own task, and not in the other app's task, then you need to specify launchMode="singleTask"
in the <activity>
declaration in the manifest for this Activity
. Then, when another app launches your Activity
, the Activity
will be launched in a separate task. If there is already an instance of the Activity
running in that task, then Android will not create a new instance of the Activity
, it will just call onNewIntent()
and deliver the Intent
that the other app used in the call to startActivity()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With