Why addDefaultArguments() removed from Navigation Component version 1.0.0-alpha09? What is WorkAround to pass arg to StartDestination from Activity?

谁说我不能喝 提交于 2019-12-10 15:13:37

问题


Hello i am working on Navigation Architecture Component and i and setting up NavHostFragment for my Activity programatically.

MainActivity.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/nav_host"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
     />
</android.support.constraint.ConstraintLayout>

MainActiviy.kt :

package com.andor.navigate.demonavigation

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setupNavigation()
        NavigationUI.setupActionBarWithNavController(this, NavHostFragment.findNavController(nav_host))

    }

    override fun onSupportNavigateUp(): Boolean = Navigation.findNavController(
        this,
        R.id.nav_host
    ).navigateUp()

    private fun setupNavigation() {
        val navHostFragment = nav_host as NavHostFragment
        val navController = navHostFragment.navController
        val navInflater = navController.navInflater
        val graph = navInflater.inflate(R.navigation.nav_graph)
        graph.addDefaultArguments(intent!!.extras!!) // This is where you pass the bundle data from Activity to StartDestination
        navHostFragment.navController.graph = graph
    }
}

Problem :

I am using the above code for a long time but i updated my gradle dependency version of android.arch.navigation:navigation from 1.0.0-alpha08 to 1.0.0-alpha09 and after update in below line of code i am getting compile time error :

code:

graph.addDefaultArguments(intent!!.extras!!) // This is where you pass the bundle data from Activity to StartDestination

error:

Unresolved reference: addDefaultArguments

dependency(project gradel file) :

dependencies {

    def nav_version = "1.0.0-alpha09"

    implementation "android.arch.navigation:navigation-fragment:$nav_version" // use -ktx for Kotlin
    implementation "android.arch.navigation:navigation-ui:$nav_version" // use -ktx for Kotlin}
}

When i revert back to navigation version 1.0.0-alpha08 everything work's fine.For some reason google have removed the addDefaultArguments from NavDestination in alpha09 version.

Anyone has idea why addDefaultArguments method is removed?

UPDATE:

What is WorkAround to pass arg to StartDestination from Activity?

Check the thread for more detail's on question basically i want to find a way to send Argument's to my startDestination from my Activity hosting the Navigation Fragment.

Before 1.0.0-alpha09 :

Only Solution can i found is using addDefaultArguments()

after 1.0.0-alpha09 no idea???

If there is any Work around to this problem Do share!


回答1:


I checked the source code an saw that there a lot of changes regarding navigation destination and arguments. I think the proper way to pass default arguments to navigation graph is something like this:

val navHostFragment = nav_host as NavHostFragment
val navController = navHostFragment.navController
val navInflater = navController.navInflater
val graph = navInflater.inflate(R.navigation.nav_graph)
if(intent.extras!=null) {
    val argument1 = intent.extras.get("Key1")
    val argument2 = intent.extras.get("Key1")
    val navArgument1=NavArgument.Builder().setDefaultValue(argument1).build()
    val navArgument2=NavArgument.Builder().setDefaultValue(argument2).build()
    graph.addArgument("Key1",navArgument1)
    graph.addArgument("Key2",navArgument2)
}
navHostFragment.navController.graph = graph

Maybe there is a better way, but i didn't found one.



来源:https://stackoverflow.com/questions/53928500/why-adddefaultarguments-removed-from-navigation-component-version-1-0-0-alpha0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!