firebase / quickstart-android

Firebase Quickstart Samples for Android
https://firebase.google.com
Apache License 2.0
8.87k stars 7.33k forks source link

Firebase Dynamic Links are reopening if put on onStart() #561

Open burnermanx opened 6 years ago

burnermanx commented 6 years ago

Environment: Android device: OnePlus 5 A5000 Android OS Version: 8.1 (27) Google Play Services version: 11.8.0 Firebase/Play Services SDK version: 11.8.0

According by Dynamic Links docs:

You must call getDynamicLink() in every activity that might be launched by the link, even though the link might be available from the intent using getIntent().getData(). Calling getDynamicLink() retrieves the link and clears that data so it is only processed once by your app.

For compatibility issues for devices which not supporting App Links, I'm loading Dynamic Links on main activity and routing to desired activity according Uri. So what I did:

@Override
public void onStart() {
    FirebaseDynamicLinks.getInstance()
                    .getDynamicLink(getIntent())
                    .addOnSuccessListener(this, pendingDynamicLinkData -> {
                        if (pendingDynamicLinkData != null) {
                            if (BuildConfig.VERSION_CODE >= pendingDynamicLinkData.getMinimumAppVersion()) {
                                routeDeepLinkIntent(this, pendingDynamicLinkData.getLink());
                            } else {
                                Intent update = pendingDynamicLinkData.getUpdateAppIntent(this);
                                startActivity(update);
                            }
                        }
                    })
                    .addOnFailureListener(this, e -> Log.w("DynLink", "On Failure", e));
}

In routeDeepLinkIntent(activity, uri) (very simplified)

if (uri.getPath().equals("activityToOpen") {
    Intent intent = new Intent(activity, ActivityToOpen.class);
    activity.startActivity(intent);
}

And works flawlessly, but, if I go back to previous activity, .getDynamicLink(getIntent()) will return dynamic link again and the process will repeat some times until stops. So the behavior is different from documentation.

If I put the code in onCreate(), dynamic link will works, but, if app is already opened, dynamic link will not work. Ok, because I ran .getDynamicLink(getIntent()) on onCreate, so activity is already created and getDynamicLink() will not be called.

How can I fix this, if possible?

samtstern commented 6 years ago

@burnermanx thanks for reporting this, I see the same behavior. I'll go find out if this is a docs bug or a product bug.

aman0987654 commented 5 years ago

Hi @samtstern , i am facing the same issue (dynamic links getting called even after exiting and reopening app from recents) in my project. Do we have a solution yet?

ZakShaker commented 4 years ago

Though it is definitely the bug, there is something you can do:

Firstly, call it onCreate() of the activity.

Secondly, if you handle dynamic links in onNewIntent()- method you can check if intent in getIntent().getData() and pendingDynamicLinkData.link are different you can decide which one to open.