我有一個簡單的應用程序,用一個單一的TextView顯示來自ACTION_SEND意圖的純文本。我的問題是,每次有一些文本被分享到這個應用程序,一個新的實例被創建。我可以在查看最近的應用程序時看到應用程序的多個實例。我在API 23如何防止從Intent.createChooser()啓動時的應用程序/活動的多個實例?
測試它這是我的代碼:
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);
}
}
}
我試圖與清單中的launchMode擺弄,但沒有一個選項來解決這個問題。
編輯1:
這裏是我的清單文件:
<?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>
發佈您的清單。 –