Navigation Architecture Component - Activities

前端 未结 3 1502
遥遥无期
遥遥无期 2021-01-31 08:08

I\'ve been following the docs from Navigation Architecture Component to understand how this new navigation system works.

To go/back from one screen to another you need

相关标签:
3条回答
  • 2021-01-31 08:10

    The navigation graph only exists within a single activity. As per the Migrate to Navigation guide, <activity> destinations can be used to start an Activity from within the navigation graph, but once that second activity is started, it is totally separate from the original navigation graph (it could have its own graph or just be a simple activity).

    You can add an Activity destination to your navigation graph via the visual editor (by hitting the + button and then selecting an activity in your project) or by manually adding the XML:

    <activity
        android:id="@+id/secondActivity"
        android:name="com.example.SecondActivity" />
    

    Then, you can navigate to that activity (i.e., start the activity) by using it just like any other destination:

    Navigation.findNavController(view).navigate(R.id.secondActivity);
    
    0 讨论(0)
  • 2021-01-31 08:20

    I managed to navigate from one activity to another without hosting a Fragment by using ActivityNavigator.

    ActivityNavigator(this)
                        .createDestination()
                        .setIntent(Intent(this, SecondActivity::class.java))
                        .navigate(null, null)
    
    0 讨论(0)
  • 2021-01-31 08:24

    I also managed to navigate from one activity to another without hosting a Fragment by using ActivityNavigator.

    Kotlin:

    val activityNavigator = ActivityNavigator( context!!)
                    activityNavigator.navigate(
                        activityNavigator.createDestination().setIntent(
                            Intent(
                                context!!,
                                SecondActivity::class.java
                            )
                        ), null, null, null
                    )
    

    Java:

    ActivityNavigator activityNavigator = new ActivityNavigator(this);
    activityNavigator.navigate(activityNavigator.createDestination().setIntent(new Intent(this, SecondActivity.class)), null, null, null);
    

    nav_graph.xml

    <fragment android:id="@+id/fragment"
            android:name="com.codingwithmitch.navigationcomponentsexample.SampleFragment"
            android:label="fragment_sample"
            tools:layout="@layout/fragment_sample">
    
        <action
                android:id="@+id/action_confirmationFragment_to_secondActivity"
                app:destination="@id/secondActivity" />
    
    </fragment>
    <activity
            android:id="@+id/secondActivity"
            android:name="com.codingwithmitch.navigationcomponentsexample.SecondActivity"
            android:label="activity_second"
            tools:layout="@layout/activity_second" />
    

    Kotlin:

    lateinit var navController: NavController
    navController = Navigation.findNavController(view)
    navController!!.navigate(R.id.action_confirmationFragment_to_secondActivity)
    
    0 讨论(0)
提交回复
热议问题