简体   繁体   English

使用 NavController 时如何使用 newInstance() 创建片段?

[英]How to create fragments with newInstance() when using NavController?

I'm creating a single-activity app that uses NavController and fragments to display each screen.我正在创建一个使用NavController和片段来显示每个屏幕的单活动应用程序。

The app has a home screen and contains three other menu items in the nav drawer so the user can visit different websites in an external browser.该应用程序有一个主屏幕,并在导航抽屉中包含其他三个菜单项,因此用户可以在外部浏览器中访问不同的网站。

In the onCreate() method of my MainActivity , I have:在我的MainActivityonCreate()方法中,我有:

mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
        R.id.nav_home,
        R.id.nav_visit_website_1,
        R.id.nav_visit_website_2,
        R.id.nav_visit_website_3)
        .setDrawerLayout(mDrawerLayout)
        .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);

I've created a VisitWebsiteFragment class which has a static newInstance(String websiteUrl) method - with the intention of using it for the three website menu items.我创建了一个VisitWebsiteFragment class ,它有一个 static newInstance(String websiteUrl)方法 - 打算将它用于三个网站菜单项。

I've read through the navigation documentation , but I'm not sure how to intercept/control the NavController in order to create the three fragment instances with VisitWebsiteFragment.newInstance(...) .我已经阅读了导航文档,但我不确定如何拦截/控制 NavController 以使用VisitWebsiteFragment.newInstance(...)创建三个片段实例。

There may be some aspect of navigation that I'm totally missing.我可能完全缺少导航的某些方面。 Could someone point me in the right direction?有人能指出我正确的方向吗?

The IDs of the menu items are nav_visit_website_1 , nav_visit_website_2 and nav_visit_website_3 .菜单项的 ID 是nav_visit_website_1nav_visit_website_2nav_visit_website_3

Adding this code to my nav_graph.xml gives me what I need:将此代码添加到我的nav_graph.xml可以满足我的需要:

<fragment
    android:id="@+id/visit_website"
    android:name="org.example.VisitWebsiteFragment"
    android:label="@string/visit_website" />

<action
    android:id="@+id/nav_visit_website_1"
    app:destination="@id/visit_website">
    <argument
        android:name="url"
        app:argType="string"
        android:defaultValue="https://example.com/website1" />
</action>

<action
    android:id="@+id/nav_visit_website_2"
    app:destination="@id/visit_website">
    <argument
        android:name="url"
        app:argType="string"
        android:defaultValue="https://example.com/website2" />
</action>

<action
    android:id="@+id/nav_visit_website_3"
    app:destination="@id/visit_website">
    <argument
        android:name="url"
        app:argType="string"
        android:defaultValue="https://example.com/website3" />
</action>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Android Studio 为 newInstance Fragments 创建 kotlin 实时模板 - How to create a kotlin live template for newInstance Fragments using Android studio 在BroadcastReceiver上使用newInstance时的InstantiationException - InstantiationException when using newInstance on BroadcastReceiver 使用 NavController 时如何返回上一个片段? - How do you return to the previous fragment when using a NavController? 使用导航控制器时如何将操作栏添加到主屏幕? - How to add Action bar to main screen when using navcontroller? Android 为什么在使用 NavController 导航时会创建两个相同的片段 - Android why are two identical Fragments created when navigating with NavController Kotlin-如何将空值传递给片段newInstance方法? - Kotlin - how to pass in null value into fragments newInstance method? 为什么总是newInstance或片段的对象 - Why are always newInstance or object of fragments 如何知道何时调用了`navController.popBackStack()`? - How to know when `navController.popBackStack()` was called? 应用程序启动时如何创建所有片段 - How to create all fragments when application start 如何取消片段导航(使用NavController) - How to cancel fragment navigation (using NavController)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM