6

In my app, I receive dynamic links within my main activity. This works great when a user opens a link and it launches the app and completes the correct actions, but the dynamic links seems to be staying around after it's been retrieved in the app.

Even after the user has pressed the link and it's retrived in the app with FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent()), the user could close the app and reopen it some time later and getDynamicLink(getIntent()) would still return the link and the data within the intent.

Is there a way to discard the link & its data once it's been retrieved once in the app? Do I just need to do setIntent(null)?

Here is my MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.ButtonTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    handleDynamicLink();
}

private void handleDynamicLink() {
    FirebaseDynamicLinks.getInstance()
            .getDynamicLink(getIntent())
            .addOnSuccessListener(this, pendingDynamicLinkData -> {
                // Get deep link from result (may be null if no link is found)
                Uri deepLink = null;
                if (pendingDynamicLinkData != null) {
                    deepLink = pendingDynamicLinkData.getLink();
                    if(deepLink!=null){
                        String gameId = deepLink.getQueryParameter("id");
                        Intent intent = new Intent(this, MultiplayerActivity.class);
                        intent.putExtra("gameId",gameId);
                        startMultiplayerActivity(intent);
                    }
                    Log.d(TAG, "handleDynamicLink: bundle: "+deepLink.getQueryParameter("id"));
                }
            }).addOnFailureListener(this, e -> Log.w(TAG, "getDynamicLink:onFailure", e));
}
Kes Walker
  • 1,154
  • 2
  • 10
  • 24
  • 4
    Does this answer your question? [Firebase dynamic link, clear it after using once](https://stackoverflow.com/questions/48128583/firebase-dynamic-link-clear-it-after-using-once) – John T Jul 30 '20 at 04:31

2 Answers2

3

after you get data from dynamic Link, you can send Data to Activity as you want. and then handle this.

after handle Data, if you want to discard Data, use this code in Activity.

getActivity().getIntent().setData(null);
S T
  • 1,068
  • 2
  • 8
  • 16
  • This isn't working for me, as I do this in `OnNewIntent()`, close my app and reopen it, the data is still there in the intent when `GetIntent()` is called within `onCreate()` – Kes Walker May 15 '20 at 14:05
  • I'm calling this in `onResume()` in my Activity and it is working. Thank you! – Dick Lucas Sep 04 '20 at 04:04
2

This is what I did to clear the intent data and extras:

// Don't reprocess if activity opened from recent apps
if (requireActivity().intent.flags.and(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) {
    Firebase.dynamicLinks
    ...
    .addOnCompleteListener {
// clear data
        requireActivity().intent.data = null
        requireActivity().intent.replaceExtras(Bundle())
    }
}

Note that I am doing this inside my fragment. If you're doing it in an activity, remove requireActivity()..

maclir
  • 3,218
  • 26
  • 39