DeepLink的使用

Navigation组件提供了对深层链接(DeepLink)的支持。通过该特性,我们可以利用PendingIntent或者一个真实的URL链接,直接跳转到应用程序的某个destination(Fragment/Activity)

最常见的两种使用场景:

1.PendingIntent的方式。当你的应用程序收到某个通知推送,你希望用户在点击该通知时,能够直接跳转到展示该通知内容的页面,那么就可以通过PendingIntent来完成此操作。

private PendingIntent getPendingIntent()
{
    if(getActivity() != null)
    {
        Bundle bundle = new Bundle();
        bundle.putString("params", "from Notification");
        return Navigation
                .findNavController(getActivity(), R.id.sendNotification)
                .createDeepLink()
                .setGraph(R.navigation.graph_deep_link_activity)
                .setDestination(R.id.deepLinkSettingsFragment)
                .setArguments(bundle)
                .createPendingIntent();
    }
    return null;
}

2.URL的方式。当用户在手机Web页面上浏览我们网站上的某个页面时,我们可以在网页上放置一个类似“在应用内打开”的按钮。当用户的手机安装有你的应用程序,通过deepLink就能打开相应的页面,如果没有安装,那么我们的网站可以导航到应用程序的下载页面,从而引导用户安装应用程序。

第一步:在导航图中为destination添加<deepLink/>标签。

<deepLink app:uri="www.YourWebsite.com/{params}" />

第二步:为相应的Activity设置<nav-graph/>标签,这样,当用户在Web中访问到你的网站时,你的应用程序便能监听到

<nav-graph android:value="@navigation/graph_deep_link_activity" />

https://zhuanlan.zhihu.com/p/69660790

扫描二维码关注公众号,回复: 12888478 查看本文章

猜你喜欢

转载自blog.csdn.net/whb008/article/details/115112570