Intent extras does not update

I am trying to get a string from a browser. I used the SEND action in AndroidManifest.xml

<intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
</intent-filter>

In MainActivity.java, I have the following code

@Override
public void onResume() {
    super.onResume();
    Intent receivedIntent=getIntent();
    etSearch.setText(getStringFromIntent(receivedIntent));
}

public String getStringFromIntent(Intent receivedIntent) {
    // get the action
    String receivedAction = receivedIntent.getAction();
    // find out what we are dealing with
    String receivedType = receivedIntent.getType();
    if (receivedAction.equals(Intent.ACTION_SEND)) {
        if (receivedType.startsWith("text/")) {
            // get the received text
            String receivedText = receivedIntent
                    .getStringExtra(Intent.EXTRA_TEXT);
            // check we have a string
            if (receivedText != null) {
                // set the text
                return receivedText.trim();
            }
        }
    }
    return "";
}

For the first time, I try to make everything work correctly. enter image description here

When I try to do this, the text is not updated enter image description here

I thought it was due to onCreate (). So I moved the code to onResume (). Nothing happened.

In accordance with this decision, I added receivedIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);and

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    this.setIntent(intent);
}

There is no effect.

What should I do to renew my intention additionally?

+3
source share
1 answer

Use FLAG_UPDATE_CURRENT when creating your PendingIntent to update additional features from a new intent.

Additional settings not deleted / replaced

or

PendingIntent.FLAG_ONE_SHOT

private void send(Intent intentShowApp) {
    PendingIntent showAppPendingIntent = PendingIntent.getActivity(context, 0, intentShowApp, PendingIntent.FLAG_ONE_SHOT);
    try {
        showAppPendingIntent.send();
    } catch (CanceledException e) {
        e.printStackTrace();
    }
}
+5

All Articles