Reputation: 1239
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>
Upvotes: 9
Views: 4764
Reputation: 95578
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()
.
Upvotes: 11