Android导航组件开放url

Android导航组件开放url,android,android-navigation,android-architecture-navigation,Android,Android Navigation,Android Architecture Navigation,我有在浏览器中打开URL的简单代码: startActivity(Intent( Intent.ACTION_VIEW, Uri.parse(resources.getString(R.string.application_url)) )) 如何将其转换为导航组件项 我是否需要为此构建导航组件,或者导航组件中内置了用于这种情况的内容?我想出来了,对于这种简单的情况,不需要自定义导航器,您只需要创建导航端点: <activity android:id="@+id/navig

我有在浏览器中打开URL的简单代码:

startActivity(Intent(
    Intent.ACTION_VIEW, Uri.parse(resources.getString(R.string.application_url))
))
如何将其转换为导航组件项


我是否需要为此构建导航组件,或者导航组件中内置了用于这种情况的内容?

我想出来了,对于这种简单的情况,不需要自定义导航器,您只需要创建导航端点:

<activity
    android:id="@+id/navigation_endpoint_id"
    app:action="android.intent.action.VIEW"
    app:data="@string/application_url"/>

如果您使用的是导航抽屉,您可以这样通过编程进行设置。通过这种方式,您可以为特定导航项设置自定义函数,而不会失去导航图的优势

navigationView.setNavigationItemSelectedListener {item->
                drawerLayout.closeDrawer(GravityCompat.START)
                when(item.itemId){
                    R.id.navigation_rate -> {
                        startActivity(Intent(
                                Intent.ACTION_VIEW, Uri.parse(resources.getString(R.string.app_playstore_url))
                        ))
                        return@setNavigationItemSelectedListener true
                    }
                    else  -> NavigationUI.onNavDestinationSelected(item, navController)
                }
            }

对于动态Uri,我们可以如下方式传递数据:

<activity
    android:id="@+id/whatsappRedirectionActivity"
    app:action="android.intent.action.VIEW"
    app:dataPattern="https://wa.me/{number}">
    <argument
         android:name="number"
         app:argType="string" />
</activity>


这需要包含到要重定向的导航图中。使用此功能,我们还可以使用safeargs操作。

如何使用动态web URL导航;我的意思是,在运行时,用户在应用程序中提供的url?我认为使用导航组件或至少在导航图范围内是不可能的。当然,您可以动态创建导航图,但随后需要重新创建活动,这不是最方便的选择。我使用导航组件已经有一段时间了,对于活动来说,最好使用老的意图。尼斯:)),谢谢。