Android Navigation Architecture Component - Is Navigation Architecture Component meant to use Single Activity Only?
Date : March 29 2020, 07:55 AM
This might help you In theory, the Navigation library supports any architecture you might want to use. Out of the box it can handle Activities and Fragments as navigation destinations, but you can plug in your own solution by implementing your own Navigator (as an example, see this article). However, quoted / paraphrased from the Google I/O talk on Navigation:
|
Simple navigation across navigation graphs with Android Navigation Architecture Component
Date : March 29 2020, 07:55 AM
will help you UPDATE: I don't recommend this approach any more. For small apps it may work for you, but I ran into complications when trying to pass data between different navigation graphs hosted inside different NavHostFragments. I think the easiest route is the "hack" of hiding fragments in your top level layout to allow a full screen view. You can add an addOnDestinationChangedListener to listen for destinations that you want full screen, and simply hide the fragment required. ORIGINAL ANSWER: I do it like this in my app and it works well. You should wrap all your current fragments in a parent fragment with a main_nav_graph.xml. For example: <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/main_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/main_nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
<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/main_nav_graph.xml"
app:startDestination="@id/main_fullscreen">
<fragment
android:id="@+id/main_fullscreen"
android:name="com.example.myapplication.MainFullscreen" >
<action
android:id="@+id/action_main_fullscreen_to_fullscreen2"
app:destination="@id/fullscreen2" />
</fragment>
<fragment
android:id="@+id/fullscreen2"
android:name="com.example.myapplication.Fullscreen2" />
</navigation>
Navigation.findNavController(activity!!, R.id.main_host_fragment).navigate(R.id.action_main_fullscreen_to_fullscreen2)
|
How to use Navigation Drawer and Bottom Navigation simultaneously - Navigation Architecture Component
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further No need of writing separate code to replace the back button with the drawer icon. In AppBarConfiguration pass the fragment ids (from nav_graph) which you are using to navigate from both bottom navigation & navigation drawer. (P.S fragments and its associated icon should have same ids) appBarConfig = AppBarConfiguration.Builder(R.id.starFragment, R.id.statsFragment, R.id.userFragment)
.setDrawerLayout(drawerLayout)
.build()
setSupportActionBar(toolbar)
setupActionBarWithNavController(navController, appBarConfig)
navView.setupWithNavController(navController)
bottomNav.setupWithNavController(navController)
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfig)
}
override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/starFragment"
android:icon="@drawable/ic_star_green_48dp"
android:title="@string/bottom_nav_title_star"
android:menuCategory="secondary"/>
<item
android:id="@+id/statsFragment"
android:icon="@drawable/ic_stats_green_48dp"
android:title="@string/bottom_nav_title_stats"
android:menuCategory="secondary"/>
<item
android:id="@+id/userFragment"
android:icon="@drawable/ic_user_green_48dp"
android:title="@string/bottom_nav_title_user"
android:menuCategory="secondary"/>
</menu>
|
Back navigation after deep link by navigation architecture component
Date : March 29 2020, 07:55 AM
|
Navigation Architecture Component - how to set/change custom back or hamburger icon with navigation controller?
Date : March 29 2020, 07:55 AM
I hope this helps . I had the same issue with navigation version 1.0.0-alpha08. I have solved it by re-implementing setupActionBarWithNavController to provide the custom behaviour that I needed. setDisplayShowTitleEnabled(false) //Disable the default title
container.findNavController().addOnDestinationChangedListener { _, destination: NavDestination, _ ->
//Set the toolbar text (I've placed a TextView within the AppBar, which is being referenced here)
toolbar_text.text = destination.label
//Set home icon
supportActionBar?.apply {
setDisplayHomeAsUpEnabled((destination.id in NO_HOME_ICON_FRAGMENTS).not())
setHomeAsUpIndicator(if (destination.id in TOP_LEVEL_FRAGMENTS) R.drawable.app_close_ic else 0)
}
}
|