pixx
pixx

Reputation: 107

Launching share-activity multiple times on Android when using the singleInstance: Getting always the first URL

I have an Android-app in which I can save URLs from the browser via the share-functionality (I created a second share-activity with a send intent-filter)

I tried to change the launchMode to all four different options, it works if on standard or top, but then for each share a single app-instance is launched.

<activity android:name=".ShareActivity" android:launchMode="singleInstance">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/*" />
    </intent-filter>
</activity>
<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

The main activity shows all the existing records, while the share-activity takes the given URL and saves it and then starts the mainactivity:

/**
 * Handle share-intent
 */
 protected void shareIntent()
 {
    String url = intent.getStringExtra(Intent.EXTRA_TEXT);  <- This is always the same when shared multiple urls
    // Here save the url etc., which works, so omitting the code

    startActivity(new Intent(getApplicationContext(), MainActivity.class)); <-- This starts the main-activity after the url has been saved
 }

/**
 * Main onCreate method, this will be called the first time when sharing
 * @param savedInstanceState
 */
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
        this.shareIntent();
 }

 // This will be called when sharing multiple times
 @Override
 protected void onResume()
 {
     super.onResume();
     this.shareIntent();
 }

When I use the standard or singleTop launchMode, the URL is saved correctly, but each time a new instance is launched. If I use singleTask or -Instance, there will be only one app-instance, but the URL is always the first one, no matter how many times I share something.

Thanks in advance

[Update]: It works! I did this:

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

And removed the call to the share-method from oncreate (since onresume gets called even the first time).

Upvotes: 2

Views: 691

Answers (0)

Related Questions