简体   繁体   English

无法使用导航组件导航到所需的片段

[英]Unable to navigate to desired fragment using navigation component

I am using navigation component in my app.我在我的应用程序中使用导航组件。 I am trying to navigate to the Fragment on click of an action bar icon, but it is not working.我试图通过单击操作栏图标导航到片段,但它不起作用。 Below is my code:下面是我的代码:

action_menu.xml action_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:title=""
    android:id="@+id/cart_notif"
    android:icon="@drawable/ic_order"
    app:showAsAction="always"
    android:actionLayout="@layout/notification_badge"/>

</menu> 

nav_host.xml nav_host.xml

 <fragment
    android:id="@+id/cartFragment"
    android:name="BottomFragments.CartFragment"
    android:label="Cart"
    tools:layout="@layout/fragment_cart">

    <action
        android:id="@+id/action_cartFragment_to_cakeFragment"
        app:destination="@id/cakeFragment"
        app:enterAnim="@anim/nav_default_enter_anim"
        app:popUpTo="@id/cakeFragment"
        app:popUpToInclusive="true" />
</fragment>

MainActivity.java MainActivity.java

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

   if(item.getItemId() == R.id.cart_notif){
       NavController navController = Navigation.findNavController(MainActivity.this,R.id.fragment);
       navController.navigate(R.id.cartFragment);
     }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onSupportNavigateUp() {
     navController.navigateUp();
    return super.onSupportNavigateUp();
}

This approach is not working - how can I navigate?这种方法不起作用 - 我该如何导航?

here I can see you are using onOptionsItemSelected for navigating from one screen to another.在这里,我可以看到您正在使用onOptionsItemSelected从一个屏幕导航到另一个屏幕。 Instead you should use onNavigationItemSelected method.相反,您应该使用onNavigationItemSelected方法。

See this answer for more details.有关更多详细信息,请参阅答案。

暂无
暂无

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

相关问题 如何在 Android 导航组件中从片段导航到活动? - How to navigate from fragment to Activity in Android navigation component? 无法使用导航情节提要板在片段中添加按钮以从一个片段跳转到另一个片段 - Unable to add a button in fragment using Navigation storyboard to jump from one fragment to another fragment 从另一个片段导航片段,例如导航链接 - Navigate fragments from another fragment like like Navigation links 使用导航组件而不使用 ViewModel 时如何在后台堆栈中保留 Fragment 实例? - How to preserve Fragment instance in backstack when using Navigation Component without using ViewModels? 使用向上按钮导航回相应的片段 - Navigate back to the respective Fragment using up button 使用FirebaseAuthListener,如果没有用户登录,它将导航到带有片段的活动 - Using FirebaseAuthListener and it will navigate to activity with fragment if there is no user logged in 有没有一种方法可以防止在使用Android导航组件时在bottomNavigation中重新创建片段 - Is there a way to prevent re-create fragment in bottomNavigation when using android navigation component 当我使用导航组件时,在启动片段之前显示 3 秒的空白屏幕? - Blank screen show for 3 second before splash fragment while i Using Navigation component? 无法处理导航片段中的后退按钮 - Unable to handle back button in navigation fragment 当点击从 WorkManager 发送的通知时,使用导航组件启动特定片段 - Launch a particular Fragment using Navigation component when tapped on Notification that is sent from WorkManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM