4

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!

Anmol
  • 8,110
  • 9
  • 38
  • 63
  • Does it matter why? They have removed it ... presumably for a good reasons ... so you will just need to deal with it. And note that an "alpha" API is liable to change. That's one of the properties of an "alpha" release. – Stephen C Dec 29 '18 at 05:42
  • yes it matter, i am using it to solve this https://stackoverflow.com/q/53913648/7972699 do you have any other way to solve this?? i can't find any other way to do the same. @StephenC – Anmol Dec 29 '18 at 05:43
  • 1
    **That is not what you asked in your Question**. You asked "why have they removed it". If that was not what you meant to ask, you should Edit your Question to remove the irrelevant stuff (and the misleading title) and ask the questions that you want answers to. – Stephen C Dec 29 '18 at 05:47
  • And besides, you have NOT explained why it matters to you **why they removed it** – Stephen C Dec 29 '18 at 05:48
  • @StephenC i will add extra information to the question but my question title is correct i want to know why is it removed as it was there till version `08` and suddenly its removed. *i will not change the title but i will add detail why i want to know that* – Anmol Dec 29 '18 at 05:55
  • 1
    In that case, my original comment stands. You don't need to know ... because knowing won't help you solve your problem. But if you *really* want to know, find the source code repo and look at the history for the class. Specifically identify the commit where the method was removed from the API and read the commit message and associated issue tracker info. – Stephen C Dec 29 '18 at 06:01
  • Ok i have well received your point i will try finding the solution of Why? and post it here and about the how to do the work around to solve my problem i will update in my question. @StephenC – Anmol Dec 29 '18 at 06:11
  • Here is the recommended approach: https://stackoverflow.com/a/57607441/3036824 – Praveen Singh Aug 22 '19 at 10:41

1 Answers1

5

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.

Alex
  • 9,102
  • 3
  • 31
  • 35