0

I inserted my app in the share list like bellow:

    <activity
        android:name="xxxx.MainActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|uiMode|orientation|screenSize|smallestScreenSize"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <meta-data
            android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
    </activity>

And I get the value from Intent like bellow:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (getIntent().getExtras() != null) {
        Intent intents = getIntent();
        String action = intents.getAction();
        String type = intents.getType();
        if (action != null && type != null) {
            String receivedUri = intents.getParcelableExtra(Intent.EXTRA_STREAM).toString();
        }
    }
}

Problem

But when my application is in recent I don't get any value from intent.What can I do?

jo jo
  • 1,758
  • 3
  • 20
  • 34
  • use shared preference for onResume cases. https://stackoverflow.com/a/10810447/7972699 – Anmol Jan 10 '19 at 16:13
  • Why are you using 'singleTask' launch mode? This will create a lot of problems. There is usually no need for this. – David Wasser Jan 10 '19 at 18:09
  • 1
    When you call `getIntent()`, this returns the `Intent` that the `Activity` was started with. If your `Activity` is still active (not finished), and you started the app without "extras" (like pressing the app icon on the HOME screen), then `getIntent()` returns an `Intent` with no extras. You need to get the extras from the `Intent` that is passed as a parameter to `onNewIntent()`. – David Wasser Jan 10 '19 at 18:12

1 Answers1

2

When your activity become visible from recent, GetIntent().getExtras() will be empty. To get new Intent, you have to override activity's 'getNewIntent' method

@Override
    protected void onNewIntent(Intent intents)
    {
        super.onNewIntent(intents);

        if (intents != null) {
        String action = intents.getAction();
        String type = intents.getType();
        if (action != null && type != null) {
            String receivedUri = intents.getParcelableExtra(Intent.EXTRA_STREAM).toString();
        }
    }
    }
Ali Ahsan
  • 985
  • 11
  • 16
  • you have typo in your code. please change 'super.onResume()' to 'super.onNewIntent(intent);' inside onNewIntent method – Ali Ahsan Jan 10 '19 at 16:29
  • 1
    This code is no different from OP's posted code. and the answer makes no sense. You say OP needs to override `getNewIntent()`, but you provide no code for that. – David Wasser Jan 10 '19 at 18:13
  • @DavidWasser thanks for pointing out. I've updated the answer – Ali Ahsan Jan 10 '19 at 20:33