airbnb / DeepLinkDispatch

A simple, annotation-based library for making deep link handling better on Android
http://nerds.airbnb.com/deeplinkdispatch/
4.39k stars 406 forks source link

No extras passed down to the activity when mode=singleTask? #170

Open antoine-dbr opened 7 years ago

antoine-dbr commented 7 years ago

Hey guys - first kudos on this library ! I am using it heavily in our Android app where we started deep-linking to a bunch of pages.

One minor issue I have is I am sending a deep-link to myself (for some OAuth flow) and the activity that needs to be linked is already around on the navigation backstack and configured as singleTask since I don't want to be created again on the nav stack when firing the deep-link.

The deep-link works as my activity onNewIntent() method get called as expected but the intent doesn't contain any extras and I am going to have to parse the intent data myself, which is too bad, since that's the big value-add of this lib.

Here are some snippets of what I am doing:

Custom annotation for starters:

package io.cens.snapshot.deeplink;

import com.airbnb.deeplinkdispatch.DeepLinkSpec;

import io.cens.snapshot.AppConfig;

@DeepLinkSpec(prefix = {AppConfig.APP_SCHEME})
public @interface SnapshotAppLink {
    String[] value();
}

I am intercepting the link from my webview and fire a deep-link to myself.

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith(AppConfig.APP_SCHEME)) {
                    final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
                    ActivityUtils.startActivity(InternalBrowser.this, intent, true);  // since we are redirecting, remove this current activity
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }

This is is activity that is configured as single task and initially pushed the webview-based activity so it's still around on the nav stack but not visible.

@SnapshotAppLink("/auth")
public class ProfileActivity extends ActivityBase implements ProfileView {

   ....

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

And this is the method that parse out that intent and would expect to have state and code as extras since they end up being serialized as query params in the deep-link url.

    public void onAuthenticated(Intent intent) {
        if (intent == null || intent.getExtras() == null)
            return;

        final Bundle extras = intent.getExtras();
        final String state = extras.getString("state");
        final String code = extras.getString("code");

        if (TextUtils.equals(state, mState)) {
            Logger.d(TAG, "Authentication completed. code=%s", code);
            // TODO: send code to server
        }
    }

Also, I may doing something wrong and in that case, I apologize for the bother. Thanks!

felipecsl commented 7 years ago

@antoine-dbr thanks for the detailed issue explanation. one quick question to make sure I understand your problem. You're saying that you're sending a deep link to yourself, apparently from the shouldOverrideUrlLoading() method above but any extras in the original intent are getting lost when going through DeepLinkDispatch. Is that correct? I don't see any extras being added in the snippet above so I got confused. If that's it, then it should be a simple fix.

antoine-dbr commented 7 years ago

Thanks @felipecsl for the quick feedback, if I put a breakpoint in that shouldOverrideUrlLoading(view, url) method, the url param looks like this:

tm-snapshot://auth?state=XXXXXXXXXXX&code=XXXXXXXXXXX

Therefore, based on the doc, I would expect DeepLinkDispatch to deserialize the state and code query params and populate an intent with state and code extras that gets delivered to my onNewIntent method.

Makes sense?

felipecsl commented 7 years ago

That's correct. You can see that behavior being tested in a unit test here. Maybe there's something wrong about your URL. Can you provide a sample one?

antoine-dbr commented 7 years ago

tm-snapshot://auth?state=uber-2490d2b5-f776-4d6e-99d3-6ba9a55e1c95&code=wawQ5wwBDXDA3A4dfpJQq7c7LJ2pSq#_