[Android Kotlin 기초] 4-2. Define Navigation path

Define Navigation path

1. NavHostFragment


!-- The NavHostFragment within the activity_main layout -->
            <fragment
                android:id="@+id/myNavHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:navGraph="@navigation/navigation"
                app:defaultNavHost="true" />

2. NavGraph


Define%20Navigation%20path%208685fa6c979e4a8d94d24c245877ba37/Untitled.png

//The complete onClickListener with Navigation
binding.playButton.setOnClickListener { view : View ->
       view.findNavController().**navigate**(**R.id.fragmentMain_to_gameFragment**)
}

3. Backstack


Define%20Navigation%20path%208685fa6c979e4a8d94d24c245877ba37/Untitled%201.png

Define%20Navigation%20path%208685fa6c979e4a8d94d24c245877ba37/Untitled%202.png

-winFragment of navGraph

<fragment
    android:id="@+id/wonFragment"
    android:name="com.jeoksyeo.mvvmpractice.WonFragment"
    tools:layout="@layout/won_fragment"
    android:label="WonFragment" >
    <action
        android:id="@+id/action_wonFragment_to_gameFragment2"
        app:popUpTo="@id/gameFragment"
        app:popUpToInclusive="true"
        app:destination="@id/gameFragment" />
</fragment>

-loseFragment of navGraph

<fragment
    android:id="@+id/loseFragment"
    android:name="com.jeoksyeo.mvvmpractice.LoseFragment"
    tools:layout="@layout/lose_fragment"
    android:label="LoseFragment" >
    <action
        android:id="@+id/action_loseFragment_to_gameFragment3"
        app:popUpTo="@id/gameFragment"
        app:destination="@id/gameFragment" />
</fragment>

4. Up button(on the app bar) versus Back button


Define%20Navigation%20path%208685fa6c979e4a8d94d24c245877ba37/Untitled%203.png

5. Add support for an Up button


-mainActivity

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding =
                DataBindingUtil.setContentView<ActivityMainBinding>(this,
                        R.layout.activity_main
                )
        
        NavigationUI.setupActionBarWithNavController(this,
                this.findNavController(R.id.nav_host_fragment))
    }

override fun onSupportNavigateUp(): Boolean {

    return this.findNavController(R.id.nav_host_fragment).navigateUp()
}

-navigationUp()

Define%20Navigation%20path%208685fa6c979e4a8d94d24c245877ba37/Untitled%204.png

6. DrawerLayout


Define%20Navigation%20path%208685fa6c979e4a8d94d24c245877ba37/Untitled%205.png

-drawerlayout.xml

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        tools:openDrawer="start"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            layout="@layout/activity_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />

    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

-activity_main_drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

        <item
            android:id="@+id/**aboutFragment**"
            android:title="@string/menu_home" />

</menu>

-navGraph

Define%20Navigation%20path%208685fa6c979e4a8d94d24c245877ba37/Untitled%206.png

-MainActivity.class

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.drawer_layout)

        navController = this.findNavController(R.id.nav_host_fragment)

        NavigationUI.setupActionBarWithNavController(
            this,
            navController,
            binding.drawerLayout
        )

        binding.navView.setNavigationItemSelectedListener(this)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {

        binding.drawerLayout.closeDrawer(GravityCompat.START)
        return NavigationUI.onNavDestinationSelected(item, navController)
                || super.onOptionsItemSelected(item)
    }

    override fun onSupportNavigateUp(): Boolean {

        return NavigationUI.navigateUp(navController, binding.drawerLayout)
    }