Wednesday 4 August 2021

How to dispatch intent to MainActivity from another activity? (react native)

I've created a custom deep link dispatcher in Java within a react native project.

The issue I'm having is that I can't get MainActivity to start from my dispatcher, and I can't quite figure out why.

I have AndroidManifest set up so that the deep link is captured in LinkDispatcherActivity effectively, but the startActivity(dispatchedIntent) never reaches MainActivity, which is where I assume it needs to go. Ordinarily in AndroidManifest.xml, deep links would be sent to MainActivity).

It does open the app, but I assume it's because LinkDispatcherActivity is part of the app? Not sure, if I say something dumb it's because this is only my 3rd day in a row writing Java 😰

Below is my code.

package com.example.app;

//Inspired by https://github.com/justeat/Android.Samples.Deeplinks, Licensed under Apache 2.0

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import net.openid.appauth.RedirectUriReceiverActivity;

public class LinkDispatcherActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            dispatchIntent(getIntent());
        } catch (IllegalArgumentException iae) {
            // Malformed URL
            if (BuildConfig.DEBUG) {
                Log.e("Deep links", "Invalid URI", iae);
            }
        } finally {
            // Always finish the activity so that it doesn't stay in our history
            finish();
        }
    }
    public void dispatchIntent(Intent intent) {
        final Uri uri = intent.getData();
        final String host = uri.getHost().toLowerCase();
        if (uri == null) throw new IllegalArgumentException("Uri cannot be null");
        // Default intent
        Intent dispatchedIntent = new Intent(LinkDispatcherActivity.this, MainActivity.class);
        dispatchedIntent.setData(uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Log.d(dispatchedIntent.getDataString(), "dispatchIntent: intent string");
        // Auth intent only, this is why I need the dispatcher
        if ("login".equals(host)) {
            dispatchedIntent = new Intent(this, RedirectUriReceiverActivity.class);
            dispatchedIntent.putExtra("requestCode", 0);
            Log.d("Login", "mapAppLink: ");
            startActivityForResult(dispatchedIntent, 0);
            return;
        }
        Log.d("Default", "mapAppLink: ");
        startActivity(dispatchedIntent);
    }
}

Edit: for the intent, I've tried: new Intent(getApplicationContext(), MainActivity.class) new Intent(reactContext, MainActivity.class) (extending ReactActivity instead of Activity) new Intent(this, MainActivity.class) new Intent("MainActivity")



from How to dispatch intent to MainActivity from another activity? (react native)

No comments:

Post a Comment