18

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 ?

2 Answers 2

21

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)
Sign up to request clarification or add additional context in comments.

5 Comments

But second navigation graph means second navigation activity, right?
Is there any way I can go to a fragment from the new graph and not to the startDestination?
@Emiliano Schiavone If you mean to a specific fragment you can add the destination fragment to your current graph. The same fragment can stay in different graph with different navigation actions.
In my case, I used a different activity because my Auth related fragments (login, signup etc) didn't have an actionbar, but my other fragments did. So adding 2 activities helped me to achieve different toolbar styles without much effort. Although I'm not sure if its a good approach.
Is there a difference between embedding a second navgraph like this, and embedding an activity that has another navgraph, and navigating to the activity instead? Android recommend the later approach here: developer.android.com/guide/navigation/navigation-migrate#add
1

You can migrate to a single Activity Navigation. In your Nav Graph add an Action to navigate between the last LoginFragemnt and MainFragment and select:

Pop Behaviour:
Pop To - Self
Inclusive - YES

This should automatically clear the stack for you and pressing back will close the App.

EDIT: Or just manually add these two lines to your nav xml under the action that moves from the LoginFragment to the MainFragment:

app:popUpTo="@id/loginFragment"
app:popUpToInclusive="true"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.