Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 473 Vote(s) - 3.63 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Navigate between different graphs with Navigation components

#1
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 ?
Reply

#2
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"
Reply

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-xml -->

<?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>

<!-- end snippet -->

Then, with your `navController`, navigate the action:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-kotlin -->

navController.navigate(R.id.action_loginFragment_to_new_graph)

<!-- end snippet -->

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through