Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigate between different graphs with Navigation components

I have two activities, one holds all the fragments for the Login process, and the other one holds all the fragments for the main app.

Let's say I want to navigate from Activity1 (that holds all navigation graph of Login) to Activity2 (That holds all the navigation graph for the main app)

  class LoginActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_login)
        }
    
        fun goToMainActivity(){
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        }
    }

Here I call the method goToMainActivity()

 class LoginFragment : Fragment() {
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.fragment_login,container,false)
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            btn_go.setOnClickListener {
                // call the method goToMainActivity() to kill all fragments contained by that Activity and move foward to MainActivity with another nav_graph
            }
        }
    }

Since LoginActivity holds a nav_graph and is the navigation host for all the Login Fragments, now I want to kill all the fragments contained to LoginActivity and move towards a new Activity (MainActivity) that holds a different nav graph

Is this the good way to do it? Or I should navigate differently ?

like image 819
SNM Avatar asked Dec 05 '19 13:12

SNM


People also ask

How do you do a navigation graph?

A navigation graph is a resource file that contains all of your app's destinations along with the logical connections, or actions, that users can take to navigate from one destination to another. You can manage your app's navigation graph using the Navigation Editor in Android Studio.


1 Answers

You don't need to define a second activity, simply add a second navigation graph to your nav_graph.xml file. Something like:

<?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/nav_graph"
            app:startDestination="@id/loginFragment">

    <fragment
            android:id="@+id/loginFragment"
            android:name="com.mycompany.loginFragment"
            tools:layout="@layout/fragment_login"
            android:label="Login" >
         <action
                android:id="@+id/action_loginFragment_to_new_graph"
                app:destination="@id/new_graph" />
    </fragment>

    <include app:graph="@navigation/new_graph" />
</navigation>

Then, with your navController, navigate the action:

navController.navigate(R.id.action_loginFragment_to_new_graph)
like image 196
Nicola Gallazzi Avatar answered Sep 29 '22 10:09

Nicola Gallazzi