Android: How to remove the reference to a firebase dynamic link after receiving it? [duplicate]

寵の児 提交于 2020-08-08 03:39:07

问题


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));
}

回答1:


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);



回答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()..



来源:https://stackoverflow.com/questions/61411945/android-how-to-remove-the-reference-to-a-firebase-dynamic-link-after-receiving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!