导航组件:如何从 activity 导航到片段
Navigation Component: how to navigate from activity to a fragment
我想在 (activity) 中单击 FAB 导航到新片段 1。
在我的主要 activity 所有页面通用的代码工厂按钮中。
谁能指导我?
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:navigationIcon="@drawable/ic_baseline_dashboard_24"
app:navigationContentDescription="Navigation icon"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottomAppBar"
android:backgroundTint="@color/colorPrimary"
style="@style/Widget.App.FloatingActionButton"
app:srcCompat="@drawable/menu_bug" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.androidfeby.bugreport.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_report"
android:name="com.androidfeby.bugreport.ui.report.FragmentReport"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_report" />
<fragment
android:id="@+id/navigation_notifications"
android:name="com.androidfeby.bugreport.ui.history.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
</navigation>
要求:如果我点击 fab 按钮,只需导航到图表中的片段“navigation_report”
主要活动:XML
单击 fab 按钮时导航到新片段
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:navigationIcon="@drawable/ic_dashboard_svgrepo_com"
app:navigationContentDescription="Navigation icon"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottomAppBar"
android:backgroundTint="@color/colorPrimary"
style="@style/Widget.App.FloatingActionButton"
app:srcCompat="@drawable/menu_bug" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
主要活动:KT
val navController = findNavController(R.id.nav_host_fragment)
// MainActivity : onCreate
// in my code fab button in the main activity common for all fragment.
floatingActionButton.setOnClickListener {
navController.navigateUp() // to clear previous navigation history
navController.navigate(R.id.new_issue)
}
// 请就此提出更好的解决方案。
mobile_navigation.XML:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/home">
<fragment
android:id="@+id/home"
android:name="com.androidfeby.bugreport.FragmentHome"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" >
</fragment>
<fragment
android:id="@+id/new_issue"
android:name="com.androidfeby.bugreport.FragmentNewIssue"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_report" />
<fragment
android:id="@+id/history"
android:name="com.androidfeby.bugreport.FragmentHistory"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
</navigation>
对于 Java 我找到了以下解决方案,它允许从 activity
进行片段导航
NavController navController = Navigation.findNavController(Activity.this, R.id.nav_host_fragment);
navController.navigateUp();
navController.navigate(R.id.FragmentYouWantShown);
感谢 Hari Shankar S 的帮助!
我想在 (activity) 中单击 FAB 导航到新片段 1。
在我的主要 activity 所有页面通用的代码工厂按钮中。
谁能指导我?
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:navigationIcon="@drawable/ic_baseline_dashboard_24"
app:navigationContentDescription="Navigation icon"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottomAppBar"
android:backgroundTint="@color/colorPrimary"
style="@style/Widget.App.FloatingActionButton"
app:srcCompat="@drawable/menu_bug" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.androidfeby.bugreport.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_report"
android:name="com.androidfeby.bugreport.ui.report.FragmentReport"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_report" />
<fragment
android:id="@+id/navigation_notifications"
android:name="com.androidfeby.bugreport.ui.history.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
</navigation>
要求:如果我点击 fab 按钮,只需导航到图表中的片段“navigation_report”
主要活动:XML 单击 fab 按钮时导航到新片段
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:navigationIcon="@drawable/ic_dashboard_svgrepo_com"
app:navigationContentDescription="Navigation icon"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottomAppBar"
android:backgroundTint="@color/colorPrimary"
style="@style/Widget.App.FloatingActionButton"
app:srcCompat="@drawable/menu_bug" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
主要活动:KT
val navController = findNavController(R.id.nav_host_fragment)
// MainActivity : onCreate
// in my code fab button in the main activity common for all fragment.
floatingActionButton.setOnClickListener {
navController.navigateUp() // to clear previous navigation history
navController.navigate(R.id.new_issue)
}
// 请就此提出更好的解决方案。
mobile_navigation.XML:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/home">
<fragment
android:id="@+id/home"
android:name="com.androidfeby.bugreport.FragmentHome"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" >
</fragment>
<fragment
android:id="@+id/new_issue"
android:name="com.androidfeby.bugreport.FragmentNewIssue"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_report" />
<fragment
android:id="@+id/history"
android:name="com.androidfeby.bugreport.FragmentHistory"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
</navigation>
对于 Java 我找到了以下解决方案,它允许从 activity
进行片段导航NavController navController = Navigation.findNavController(Activity.this, R.id.nav_host_fragment);
navController.navigateUp();
navController.navigate(R.id.FragmentYouWantShown);
感谢 Hari Shankar S 的帮助!