AhmadReza Payan
AhmadReza Payan

Reputation: 2271

Android: onActivityResult Intent is null when activity get started through the browser-link

I'd like to implement a plugin for my Cordova/Ionic Android application. I have these two main files in the plugin:

1. MyPlugin.java Which will be called through an Ionic (Ionic 3, Angular 5) application.

2. BrowserLinkActivity.java Is responsible for starting a browser and getting data back from the browser.

MyPlugin.java :

The plugin will start the BrowserLinkActivity with these lines of code:

Intent intent = new Intent(context, BrowserLinkActivity.class);
cordova.startActivityForResult((CordovaPlugin) this, intent, REQUEST_CODE);

Which accordingly, the onActivityResult is overridden in the plugin:

@Override
public void onActivityResult( int requestCode, int resultCode, Intent data) {
 // The data parameter is always null
}

Here is the activity. The browser Intent will start in BrowserLinkActivity activity onCreate.

BrowserLinkActivity.java :

public class BrowserLinkActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    Uri data = getIntent().getData();
    if (data == null){

      Intent browserIntent = new Intent(Intent.ACTION_VIEW, "The website URL");

      browserIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | 
                Intent.FLAG_ACTIVITY_NO_HISTORY | 
                Intent.FLAG_FROM_BACKGROUND | 
                Intent.FLAG_ACTIVITY_CLEAR_TOP);

      startActivity(browserIntent);
    }
    else { // Activity has started through the browser-link
        Intent returnIntent = new Intent();             
        returnIntent.putExtra("Key","Something");

        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }

}

I have defined the plugin.xml file like below:

<config-file target="AndroidManifest.xml" parent="/manifest/application">
    <activity android:name="cordova.plugin.mycustomplugin.BrowserLinkActivity"
       android:launchMode="singleTask" android:noHistory="true">

        <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="http" android:host="MyHost.COM" />
        </intent-filter>

    </activity>
</config-file>

However, the problem is that when the activity gets called from browser-link, the setResult method doesn't work and Intent parameter in onActivityResult is always NULL (and the requestCode is RESULT_CANCELED, too).

How could I prevent browser-link from starting/creating a new Instance of BrowserLinkActivity activity? (which I think is the main reason that causes setResult not works properly).

Note: I've already added Intent.FLAG_ACTIVITY_SINGLE_TOP and Intent.FLAG_ACTIVITY_CLEAR_TOP to the browserIntent and added android:launchMode="singleTask" for the BrowserLinkActivity in the plugin.xml but still doesn't working.

Any help is appreciated.

Upvotes: 1

Views: 764

Answers (1)

Reaz Murshed
Reaz Murshed

Reputation: 24211

I had suggested earlier to set the launchMode to SingleTask for BrowserLinkActivity.

However, later the author of the question found that removing android:noHistory="true" from the plugin.xml and lanching the BrowserLinkActivity activity with these two intent flags (i.e. FLAG_ACTIVITY_SINGLE_TOP | FLAG_ACTIVITY_CLEAR_TOP) worked in this case and the result was set back perfectly.

Hope that helps other developers in future.

Upvotes: 1

Related Questions