Title: Receiving Simple Data Through Intent for Android App while App is in background
Tags: android;android-intent
Question: I have an app that receives simple data (text) from other apps through the other app sharing to my app. Part of the manifest looks like this:
<activity
android:name=".presentation.ui.activities.MainActivity"
android:launchMode="singleTop"&gt;
<intent-filter&gt;
<action android:name="android.intent.action.MAIN" /&gt;
<category android:name="android.intent.category.LAUNCHER" /&gt;
</intent-filter&gt;
<intent-filter&gt;
<action android:name="android.intent.action.SEND" /&gt;
<category android:name="android.intent.category.DEFAULT" /&gt;
<data android:mimeType="text/plain" /&gt;
</intent-filter&gt;
</activity&gt;
When the activity hasn't been created yet, I share the text data from the other app to my app and handle the intent with action intent.action.Send in the onCreate method. However, I also want to be able to share the text data while the app is in the background. When the app is in the background and I share the text data from the other app, the action is intent.action.MAIN in the onRestart or onResume methods.
This is the handleIntent method
private void handleIntent(){
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) &amp;&amp; type != null) {
if ("text/plain".equals(type)) {
String text = intent.getStringExtra(Intent.EXTRA_TEXT);
// logic
}
How do I receive the text data from the other app while my app is in the background?


Here is another answer: I have figured it out.
I overrode the newIntent method
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);

Here is another answer: Sending Simple Data to Other Apps
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
Receiving Simple Data from Other Apps
Update Your Manifest
<intent-filter&gt;
<action android:name="android.intent.action.SEND" /&gt;
<category android:name="android.intent.category.DEFAULT" /&gt;
<data android:mimeType="text/plain" /&gt;
</intent-filter&gt;
Handle the Incoming Content
void onCreate (Bundle savedInstanceState) {
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) &amp;&amp; type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
Comment for this answer: you need to refresh activity
Comment for this answer: The onCreate method isn't called when the activity is brought back into the foreground. And the intent that is sent is intent.action.Main.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:01/27/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><android-intent>', 'Title': 'Receiving Simple Data Through Intent for Android App while App is in background', 'ViewCount': 1940, 'id': 48472474, 'user_AboutMe': None, 'user_AccountId': 12042484.0, 'user_DisplayName': 'shusnain', 'user_Reputation': 21.0, 'user_Views': 3.0, 'user_id': 8808917.0}}