15

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 ?

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
SNM
  • 5,625
  • 9
  • 28
  • 77

2 Answers2

18

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)
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
  • 2
    But second navigation graph means second navigation activity, right? – StayCool Jul 23 '20 at 09:05
  • 3
    Is there any way I can go to a fragment from the new graph and not to the startDestination? – Emiliano Schiavone Mar 12 '21 at 14:14
  • 1
    @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. – Nicola Gallazzi Mar 12 '21 at 14:22
  • 1
    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. – Ali Akber May 19 '21 at 06:13
  • 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: https://developer.android.com/guide/navigation/navigation-migrate#add – Chucky Feb 04 '22 at 11:18
0

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"
Rafael Skubisz
  • 460
  • 3
  • 9