{"id":330375,"date":"2022-03-06T21:00:09","date_gmt":"2022-03-06T21:00:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=330375"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=330375","title":{"rendered":"<span>Single Activity \u0441 Navigation Component. \u0418\u043b\u0438 \u043a\u0430\u043a \u044f \u043c\u0443\u0447\u0430\u043b\u0441\u044f \u0441 \u0433\u0440\u0430\u0444\u0430\u043c\u0438. Boilerplate \u0447. 1<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0410\u043b\u0438\u0448\u0435\u0440, Android-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0443\u0436\u0435 \u043a\u0430\u043a 1,5 \u0433\u043e\u0434\u0430. \u0417\u0430 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 (Boilerplate) \u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0443 \u043d\u0430\u0441 \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0410 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u0438 \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044f \u0435\u043b Single Activity Architecture \u0441 Fragment&#8217;\u0430\u043c\u0438 \u0438 Navigation Component.<\/p>\n<p>\u0414\u043b\u044f \u043e\u0431\u0449\u0435\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e \u043f\u0440\u043e Single Activity, <a href=\"https:\/\/habr.com\/ru\/company\/redmadrobot\/blog\/426617\/\" rel=\"noopener noreferrer nofollow\">\u041b\u0438\u0446\u0435\u043d\u0437\u0438\u044f \u043d\u0430 \u0432\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u0438\u0434\u0430, \u0438\u043b\u0438 \u043f\u043e\u0447\u0435\u043c\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c Single-Activity<\/a>, \u0438 \u0434\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/hh\/blog\/535534\/\" rel=\"noopener noreferrer nofollow\">Navigation Component-\u0434\u0437\u044e\u0446\u0443<\/a>.<\/p>\n<p>\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Single Activity \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0432\u043e\u043f\u0440\u043e\u0441, \u043d\u0430 \u0447\u0442\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c Activity? \u041e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u0432\u044b\u0448\u0435\u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c Activity \u043d\u0430 FlowFragment&#8217;\u044b, \u0430 \u0447\u0442\u043e \u044d\u0442\u043e? \u042d\u0442\u043e Fragment \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e Activity. \u0412 Navigation Component \u044d\u0442\u043e \u0443 \u043d\u0430\u0441 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0441\u043e \u0441\u0432\u043e\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u043c \u0438 \u0433\u0440\u0430\u0444\u043e\u043c. \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u0439 \u043a\u043e\u0434, \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441:<\/p>\n<pre><code class=\"kotlin\">abstract class BaseFlowFragment(     @LayoutRes layoutId: Int,     @IdRes private val navHostFragmentId: Int ) : Fragment(layoutId) {      final override fun onViewCreated(view: View, savedInstanceState: Bundle?) {         super.onViewCreated(view, savedInstanceState)          val navHostFragment =             childFragmentManager.findFragmentById(navHostFragmentId) as NavHostFragment         val navController = navHostFragment.navController          setupNavigation(navController)     }      protected open fun setupNavigation(navController: NavController) {     } }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 <code>navController<\/code>&#8216;a, \u043d\u0443\u0436\u043d\u043e \u0443\u0442\u043e\u0447\u043d\u0438\u0442\u044c \u043c\u043e\u043c\u0435\u043d\u0442 \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 Activity, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>navController<\/code>&#8216;a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>childFragmentManager<\/code>.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a\u0430\u043a \u044d\u0442\u043e \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435. \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0444\u043b\u043e\u0443 \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \/ \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0438 \u0413\u043b\u0430\u0432\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0441 \u043d\u0438\u0436\u043d\u0435\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0435\u0439.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c <code>SignFlowFragment<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \/ \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044e. \u0418 <code>MainFlowFragment<\/code> \u0434\u043b\u044f \u0413\u043b\u0430\u0432\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0441 \u043d\u0438\u0436\u043d\u0435\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0435\u0439.<\/p>\n<pre><code class=\"kotlin\">class SignFlowFragment : BaseFlowFragment(     R.layout.flow_fragment_sign, R.id.nav_host_fragment_sign )  class MainFlowFragment : BaseFlowFragment(     R.layout.flow_fragment_main, R.id.nav_host_fragment_main ) {          private val binding by viewBinding(FlowFragmentMainBinding::bind)      override fun setupNavigation(navController: NavController) {         binding.bottomNavigation.setupWithNavController(navController)     } }<\/code><\/pre>\n<pre><code class=\"xml\">&lt;FrameLayout 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=\".presentation.ui.fragments.sign.SignFlowFragment\">      &lt;androidx.fragment.app.FragmentContainerView         android:id=\"@+id\/nav_host_fragment_sign\"         android:name=\"androidx.navigation.fragment.NavHostFragment\"         android:layout_width=\"match_parent\"         android:layout_height=\"match_parent\"         app:defaultNavHost=\"true\"         app:navGraph=\"@navigation\/sign_graph\" \/>   &lt;\/FrameLayout>    &lt;androidx.constraintlayout.widget.ConstraintLayout 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=\".presentation.ui.fragments.main.MainFlowFragment\">      &lt;androidx.fragment.app.FragmentContainerView         android:id=\"@+id\/nav_host_fragment_main\"         android:name=\"androidx.navigation.fragment.NavHostFragment\"         android:layout_width=\"0dp\"         android:layout_height=\"match_parent\"         app:defaultNavHost=\"true\"         app:layout_constraintBottom_toTopOf=\"@id\/bottom_navigation\"         app:layout_constraintTop_toTopOf=\"parent\"         app:navGraph=\"@navigation\/main_graph\" \/>      &lt;com.google.android.material.bottomnavigation.BottomNavigationView         android:id=\"@+id\/bottom_navigation\"         android:layout_width=\"match_parent\"         android:layout_height=\"wrap_content\"         app:layout_constraintBottom_toBottomOf=\"parent\"         app:menu=\"@menu\/menu_bottom_navigation\" \/>   &lt;\/androidx.constraintlayout.widget.ConstraintLayout><\/code><\/pre>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c <code>SignIn<\/code> \u0438 <code>SignUp<\/code> fragment&#8217;\u044b. \u0418 \u0432\u044b\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044e \u0432\u043d\u0443\u0442\u0440\u0438 <code>sign_graph<\/code>. \u041a\u0435\u0439\u0441 \u0442\u0430\u043a\u043e\u0439 \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0432\u0438\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 <code>SignIn<\/code> \u0432 <code>SignUp<\/code> \u0438 <code>MainFlowFragment<\/code>. \u0410 \u043a\u0430\u043a \u043d\u0430\u0432\u0438\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0436\u0434\u0443 FlowFragment&#8217;\u0430\u043c\u0438. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c kotlin file <code>NavigationExtensions<\/code>:<\/p>\n<pre><code class=\"kotlin\">fun Fragment.activityNavController() = requireActivity().findNavController(R.id.nav_host_fragment)  fun NavController.navigateSafely(@IdRes actionId: Int) {     currentDestination?.getAction(actionId)?.let { navigate(actionId) } }  fun NavController.navigateSafely(directions: NavDirections) {     currentDestination?.getAction(directions.actionId)?.let { navigate(directions) } }<\/code><\/pre>\n<p><code>activityNavController<\/code> \u044d\u0442\u043e \u0443 \u043d\u0430\u0441 <code>navController<\/code> <code>MainActivity<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u043d\u0430\u0432\u0438\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0436\u0434\u0443 FlowFragment&#8217;\u0430\u043c\u0438. \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0432\u0430 extension&#8217;a \u0434\u043b\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u0438 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 (\u043b\u0438\u0431\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043d\u0430\u0436\u0430\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u0443 \u043a\u043d\u043e\u043f\u043a\u0443 \u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u043c, \u043b\u0438\u0431\u043e \u0434\u0432\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u043a\u043d\u043e\u043f\u043a\u0438 \u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430\u043c\u0438) \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043a\u0440\u0430\u0448 <code>IllegalArgumentException<\/code>.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043a\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f \u0441 <code>SignInFragment<\/code><\/p>\n<pre><code class=\"kotlin\">private fun clickSignIn() {     binding.buttonSignIn.setOnClickListener {         UserData.isAuthorized = true         activityNavController().navigateSafely(R.id.action_global_mainFlowFragment)     } }  private fun clickSignUp() {     binding.buttonSignUp.setOnClickListener {         findNavController().navigateSafely(R.id.action_signInFragment_to_signUpFragment)     } }<\/code><\/pre>\n<p>\u041d\u043e \u0432\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u043a\u0430\u043a \u044d\u0442\u043e \u0432\u0441\u0435 \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u0432 <code>MainActivity<\/code> \u0438 \u043a\u0430\u043a\u043e\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u043a\u0440\u044b\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u043c, \u0442\u0430\u043a\u043e\u0439 \u043a\u0435\u0439\u0441 \u043c\u044b \u0440\u0435\u0448\u0438\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0435\u0442\u0442\u0438\u043d\u0433\u0430 <code>startDestination<\/code>&#8216;\u0430. \u041f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u0431\u0440\u0430\u0442\u044c <code>app:startDestination<\/code> \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u0438 <code>app:navGraph<\/code> \u0441 <code>FragmentContainerView<\/code>.<\/p>\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?> &lt;FrameLayout 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=\".presentation.ui.activity.MainActivity\">      &lt;androidx.fragment.app.FragmentContainerView         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\" \/>   &lt;\/FrameLayout><\/code><\/pre>\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?> &lt;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\/nav_graph\"     tools:ignore=\"InvalidNavigation\">      &lt;action         android:id=\"@+id\/action_global_signFlowFragment\"         app:destination=\"@id\/signFlowFragment\"         app:popUpTo=\"@id\/nav_graph\" \/>      &lt;action         android:id=\"@+id\/action_global_mainFlowFragment\"         app:destination=\"@id\/mainFlowFragment\"         app:popUpTo=\"@id\/nav_graph\" \/>      &lt;fragment         android:id=\"@+id\/mainFlowFragment\"         android:name=\"com.alish.navigationflowsample.presentation.ui.fragments.main.MainFlowFragment\"         android:label=\"flow_fragment_main\"         tools:layout=\"@layout\/flow_fragment_main\" \/>      &lt;fragment         android:id=\"@+id\/signFlowFragment\"         android:name=\"com.alish.navigationflowsample.presentation.ui.fragments.sign.SignFlowFragment\"         android:label=\"flow_fragment_sign\"         tools:layout=\"@layout\/flow_fragment_sign\" \/>   &lt;\/navigation><\/code><\/pre>\n<p>\u041a\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>navController<\/code>&#8216;a \u0432 <code>MainActivity<\/code><\/p>\n<pre><code class=\"kotlin\">private fun setupNavigation() {     val navHostFragment =         supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment     navController = navHostFragment.navController      val navGraph = navController.navInflater.inflate(R.navigation.nav_graph)     when {         UserData.isAuthorized -> {             navGraph.setStartDestination(R.id.mainFlowFragment)         }         !UserData.isAuthorized -> {             navGraph.setStartDestination(R.id.signFlowFragment)         }     }     navController.graph = navGraph }<\/code><\/pre>\n<p>\u041f\u043b\u044e\u0441 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430. \u041c\u044b \u0440\u0435\u0448\u0430\u0435\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 SharedViewModel&#8217;a \u0441 Single Activity. \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 by activityViewModels, \u043d\u0430\u0448 ViewModel \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f Singleton&#8217;\u043e\u043c \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 ViewModel \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0438 activity, \u0430 \u043e\u043d \u0443 \u043d\u0430\u0441 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043d\u0430 \u0432\u0441\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0420\u0435\u0448\u0430\u0435\u043c \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/developer.android.com\/guide\/navigation\/navigation-programmatic#share_ui-related_data_between_destinations_with_viewmodel\" rel=\"noopener noreferrer nofollow\">navGraphViewModels<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/developer.android.com\/training\/dependency-injection\/hilt-jetpack#viewmodel-navigation\" rel=\"noopener noreferrer nofollow\">hiltNavGraphViewModels<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043a \u0433\u0440\u0430\u0444\u0443 \u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u044e\u0442\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043d\u0438\u043c\u0438.<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0441\u0435\u0433\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<div class=\"embed_link\">\n<div class=\"embed__thumb\" style=\"background-image: url(&quot;https:\/\/lh3.googleusercontent.com\/NaVOLjcn9l3lrm7IoTtvf5haJMRBxPhMzhx8r0rO-5bDWcnmkyABOtAux2Fq1e-d9iI=w1200-h630-p&quot;);\"><\/div>\n<div class=\"embed__caption\">\n<div class=\"embed__caption-title\"><span>NavigationFlowSample.mp4<\/span><\/div>\n<p><a href=\"https:\/\/drive.google.com\/file\/d\/1MHYJ4Jvnr2CrVRqIZHa2c57OEqGOdIz6\/view?usp=sharing\" target=\"_blank\" rel=\"noopener noreferrer nofollow\" class=\"embed__caption-link\">drive.google.com<\/a><\/div>\n<\/div>\n<p>P.S. \u0418 \u0434\u0430, \u043f\u0435\u0440\u0435\u0435\u0437\u0436\u0430\u0435\u043c \u043d\u0430 Compose \u0438 \u0437\u0430\u0447\u0435\u043c \u0432\u0441\u0435 \u044d\u0442\u043e \ud83d\ude42 \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u044b, \u043e\u0442\u043a\u0440\u044b\u0442 \u043a \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u043a\u0440\u0438\u0442\u0438\u043a\u0435.<\/p>\n<p><a href=\"http:\/\/github.com\/TheAlisher\/NavigationFlowSample.git\" rel=\"noopener noreferrer nofollow\">\u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442<\/a><br \/><a href=\"http:\/\/github.com\/TheAlisher\/Boilerplate-Android.git\" rel=\"noopener noreferrer nofollow\">Boilerplate<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/654599\/\"> https:\/\/habr.com\/ru\/post\/654599\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0410\u043b\u0438\u0448\u0435\u0440, Android-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0443\u0436\u0435 \u043a\u0430\u043a 1,5 \u0433\u043e\u0434\u0430. \u0417\u0430 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 (Boilerplate) \u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0443 \u043d\u0430\u0441 \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0410 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u0438 \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044f \u0435\u043b Single Activity Architecture \u0441 Fragment&#8217;\u0430\u043c\u0438 \u0438 Navigation Component.<\/p>\n<p>\u0414\u043b\u044f \u043e\u0431\u0449\u0435\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e \u043f\u0440\u043e Single Activity, <a href=\"https:\/\/habr.com\/ru\/company\/redmadrobot\/blog\/426617\/\" rel=\"noopener noreferrer nofollow\">\u041b\u0438\u0446\u0435\u043d\u0437\u0438\u044f \u043d\u0430 \u0432\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u0438\u0434\u0430, \u0438\u043b\u0438 \u043f\u043e\u0447\u0435\u043c\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c Single-Activity<\/a>, \u0438 \u0434\u043b\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/hh\/blog\/535534\/\" rel=\"noopener noreferrer nofollow\">Navigation Component-\u0434\u0437\u044e\u0446\u0443<\/a>.<\/p>\n<p>\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Single Activity \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0432\u043e\u043f\u0440\u043e\u0441, \u043d\u0430 \u0447\u0442\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c Activity? \u041e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u0432\u044b\u0448\u0435\u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c Activity \u043d\u0430 FlowFragment&#8217;\u044b, \u0430 \u0447\u0442\u043e \u044d\u0442\u043e? \u042d\u0442\u043e Fragment \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e Activity. \u0412 Navigation Component \u044d\u0442\u043e \u0443 \u043d\u0430\u0441 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0441\u043e \u0441\u0432\u043e\u0438\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u043c \u0438 \u0433\u0440\u0430\u0444\u043e\u043c. \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u0439 \u043a\u043e\u0434, \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441:<\/p>\n<pre><code class=\"kotlin\">abstract class BaseFlowFragment(     @LayoutRes layoutId: Int,     @IdRes private val navHostFragmentId: Int ) : Fragment(layoutId) {      final override fun onViewCreated(view: View, savedInstanceState: Bundle?) {         super.onViewCreated(view, savedInstanceState)          val navHostFragment =             childFragmentManager.findFragmentById(navHostFragmentId) as NavHostFragment         val navController = navHostFragment.navController          setupNavigation(navController)     }      protected open fun setupNavigation(navController: NavController) {     } }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 <code>navController<\/code>&#8216;a, \u043d\u0443\u0436\u043d\u043e \u0443\u0442\u043e\u0447\u043d\u0438\u0442\u044c \u043c\u043e\u043c\u0435\u043d\u0442 \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 Activity, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <code>navController<\/code>&#8216;a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>childFragmentManager<\/code>.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a\u0430\u043a \u044d\u0442\u043e \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435. \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0444\u043b\u043e\u0443 \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \/ \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0438 \u0413\u043b\u0430\u0432\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0441 \u043d\u0438\u0436\u043d\u0435\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0435\u0439.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c <code>SignFlowFragment<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \/ \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044e. \u0418 <code>MainFlowFragment<\/code> \u0434\u043b\u044f \u0413\u043b\u0430\u0432\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0441 \u043d\u0438\u0436\u043d\u0435\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0435\u0439.<\/p>\n<pre><code class=\"kotlin\">class SignFlowFragment : BaseFlowFragment(     R.layout.flow_fragment_sign, R.id.nav_host_fragment_sign )  class MainFlowFragment : BaseFlowFragment(     R.layout.flow_fragment_main, R.id.nav_host_fragment_main ) {          private val binding by viewBinding(FlowFragmentMainBinding::bind)      override fun setupNavigation(navController: NavController) {         binding.bottomNavigation.setupWithNavController(navController)     } }<\/code><\/pre>\n<pre><code class=\"xml\">&lt;FrameLayout 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=\".presentation.ui.fragments.sign.SignFlowFragment\">      &lt;androidx.fragment.app.FragmentContainerView         android:id=\"@+id\/nav_host_fragment_sign\"         android:name=\"androidx.navigation.fragment.NavHostFragment\"         android:layout_width=\"match_parent\"         android:layout_height=\"match_parent\"         app:defaultNavHost=\"true\"         app:navGraph=\"@navigation\/sign_graph\" \/>   &lt;\/FrameLayout>    &lt;androidx.constraintlayout.widget.ConstraintLayout 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=\".presentation.ui.fragments.main.MainFlowFragment\">      &lt;androidx.fragment.app.FragmentContainerView         android:id=\"@+id\/nav_host_fragment_main\"         android:name=\"androidx.navigation.fragment.NavHostFragment\"         android:layout_width=\"0dp\"         android:layout_height=\"match_parent\"         app:defaultNavHost=\"true\"         app:layout_constraintBottom_toTopOf=\"@id\/bottom_navigation\"         app:layout_constraintTop_toTopOf=\"parent\"         app:navGraph=\"@navigation\/main_graph\" \/>      &lt;com.google.android.material.bottomnavigation.BottomNavigationView         android:id=\"@+id\/bottom_navigation\"         android:layout_width=\"match_parent\"         android:layout_height=\"wrap_content\"         app:layout_constraintBottom_toBottomOf=\"parent\"         app:menu=\"@menu\/menu_bottom_navigation\" \/>   &lt;\/androidx.constraintlayout.widget.ConstraintLayout><\/code><\/pre>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c <code>SignIn<\/code> \u0438 <code>SignUp<\/code> fragment&#8217;\u044b. \u0418 \u0432\u044b\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044e \u0432\u043d\u0443\u0442\u0440\u0438 <code>sign_graph<\/code>. \u041a\u0435\u0439\u0441 \u0442\u0430\u043a\u043e\u0439 \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0432\u0438\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 <code>SignIn<\/code> \u0432 <code>SignUp<\/code> \u0438 <code>MainFlowFragment<\/code>. \u0410 \u043a\u0430\u043a \u043d\u0430\u0432\u0438\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0436\u0434\u0443 FlowFragment&#8217;\u0430\u043c\u0438. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c kotlin file <code>NavigationExtensions<\/code>:<\/p>\n<pre><code class=\"kotlin\">fun Fragment.activityNavController() = requireActivity().findNavController(R.id.nav_host_fragment)  fun NavController.navigateSafely(@IdRes actionId: Int) {     currentDestination?.getAction(actionId)?.let { navigate(actionId) } }  fun NavController.navigateSafely(directions: NavDirections) {     currentDestination?.getAction(directions.actionId)?.let { navigate(directions) } }<\/code><\/pre>\n<p><code>activityNavController<\/code> \u044d\u0442\u043e \u0443 \u043d\u0430\u0441 <code>navController<\/code> <code>MainActivity<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u043d\u0430\u0432\u0438\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0436\u0434\u0443 FlowFragment&#8217;\u0430\u043c\u0438. \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0432\u0430 extension&#8217;a \u0434\u043b\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u0438 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 (\u043b\u0438\u0431\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043d\u0430\u0436\u0430\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u0443 \u043a\u043d\u043e\u043f\u043a\u0443 \u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u043c, \u043b\u0438\u0431\u043e \u0434\u0432\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u043a\u043d\u043e\u043f\u043a\u0438 \u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430\u043c\u0438) \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043a\u0440\u0430\u0448 <code>IllegalArgumentException<\/code>.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043a\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f \u0441 <code>SignInFragment<\/code><\/p>\n<pre><code class=\"kotlin\">private fun clickSignIn() {     binding.buttonSignIn.setOnClickListener {         UserData.isAuthorized = true         activityNavController().navigateSafely(R.id.action_global_mainFlowFragment)     } }  private fun clickSignUp() {     binding.buttonSignUp.setOnClickListener {         findNavController().navigateSafely(R.id.action_signInFragment_to_signUpFragment)     } }<\/code><\/pre>\n<p>\u041d\u043e \u0432\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u043a\u0430\u043a \u044d\u0442\u043e \u0432\u0441\u0435 \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u0432 <code>MainActivity<\/code> \u0438 \u043a\u0430\u043a\u043e\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0442\u043a\u0440\u044b\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u043c, \u0442\u0430\u043a\u043e\u0439 \u043a\u0435\u0439\u0441 \u043c\u044b \u0440\u0435\u0448\u0438\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0435\u0442\u0442\u0438\u043d\u0433\u0430 <code>startDestination<\/code>&#8216;\u0430. \u041f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u0431\u0440\u0430\u0442\u044c <code>app:startDestination<\/code> \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u0438 <code>app:navGraph<\/code> \u0441 <code>FragmentContainerView<\/code>.<\/p>\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?> &lt;FrameLayout 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=\".presentation.ui.activity.MainActivity\">      &lt;androidx.fragment.app.FragmentContainerView         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\" \/>   &lt;\/FrameLayout><\/code><\/pre>\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?> &lt;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\/nav_graph\"     tools:ignore=\"InvalidNavigation\">      &lt;action         android:id=\"@+id\/action_global_signFlowFragment\"         app:destination=\"@id\/signFlowFragment\"         app:popUpTo=\"@id\/nav_graph\" \/>      &lt;action         android:id=\"@+id\/action_global_mainFlowFragment\"         app:destination=\"@id\/mainFlowFragment\"         app:popUpTo=\"@id\/nav_graph\" \/>      &lt;fragment         android:id=\"@+id\/mainFlowFragment\"         android:name=\"com.alish.navigationflowsample.presentation.ui.fragments.main.MainFlowFragment\"         android:label=\"flow_fragment_main\"         tools:layout=\"@layout\/flow_fragment_main\" \/>      &lt;fragment         android:id=\"@+id\/signFlowFragment\"         android:name=\"com.alish.navigationflowsample.presentation.ui.fragments.sign.SignFlowFragment\"         android:label=\"flow_fragment_sign\"         tools:layout=\"@layout\/flow_fragment_sign\" \/>   &lt;\/navigation><\/code><\/pre>\n<p>\u041a\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>navController<\/code>&#8216;a \u0432 <code>MainActivity<\/code><\/p>\n<pre><code class=\"kotlin\">private fun setupNavigation() {     val navHostFragment =         supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment     navController = navHostFragment.navController      val navGraph = navController.navInflater.inflate(R.navigation.nav_graph)     when {         UserData.isAuthorized -> {             navGraph.setStartDestination(R.id.mainFlowFragment)         }         !UserData.isAuthorized -> {             navGraph.setStartDestination(R.id.signFlowFragment)         }     }     navController.graph = navGraph }<\/code><\/pre>\n<p>\u041f\u043b\u044e\u0441 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430. \u041c\u044b \u0440\u0435\u0448\u0430\u0435\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 SharedViewModel&#8217;a \u0441 Single Activity. \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 by activityViewModels, \u043d\u0430\u0448 ViewModel \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f Singleton&#8217;\u043e\u043c \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 ViewModel \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0438 activity, \u0430 \u043e\u043d \u0443 \u043d\u0430\u0441 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043d\u0430 \u0432\u0441\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0420\u0435\u0448\u0430\u0435\u043c \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/developer.android.com\/guide\/navigation\/navigation-programmatic#share_ui-related_data_between_destinations_with_viewmodel\" rel=\"noopener noreferrer nofollow\">navGraphViewModels<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/developer.android.com\/training\/dependency-injection\/hilt-jetpack#viewmodel-navigation\" rel=\"noopener noreferrer nofollow\">hiltNavGraphViewModels<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043a \u0433\u0440\u0430\u0444\u0443 \u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u044e\u0442\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043d\u0438\u043c\u0438.<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0441\u0435\u0433\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<div class=\"embed_link\">\n<div class=\"embed__thumb\" style=\"background-image: url(&quot;https:\/\/lh3.googleusercontent.com\/NaVOLjcn9l3lrm7IoTtvf5haJMRBxPhMzhx8r0rO-5bDWcnmkyABOtAux2Fq1e-d9iI=w1200-h630-p&quot;);\"><\/div>\n<div class=\"embed__caption\">\n<div class=\"embed__caption-title\"><span>NavigationFlowSample.mp4<\/span><\/div>\n<p><a href=\"https:\/\/drive.google.com\/file\/d\/1MHYJ4Jvnr2CrVRqIZHa2c57OEqGOdIz6\/view?usp=sharing\" target=\"_blank\" rel=\"noopener noreferrer nofollow\" class=\"embed__caption-link\">drive.google.com<\/a><\/div>\n<\/div>\n<p>P.S. \u0418 \u0434\u0430, \u043f\u0435\u0440\u0435\u0435\u0437\u0436\u0430\u0435\u043c \u043d\u0430 Compose \u0438 \u0437\u0430\u0447\u0435\u043c \u0432\u0441\u0435 \u044d\u0442\u043e \ud83d\ude42 \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u044b, \u043e\u0442\u043a\u0440\u044b\u0442 \u043a \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u043a\u0440\u0438\u0442\u0438\u043a\u0435.<\/p>\n<p><a href=\"http:\/\/github.com\/TheAlisher\/NavigationFlowSample.git\" rel=\"noopener noreferrer nofollow\">\u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442<\/a><br \/><a href=\"http:\/\/github.com\/TheAlisher\/Boilerplate-Android.git\" rel=\"noopener noreferrer nofollow\">Boilerplate<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/654599\/\"> https:\/\/habr.com\/ru\/post\/654599\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-330375","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/330375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=330375"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/330375\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}