Waqas Younis
Waqas Younis

Reputation: 94

Call function/method of the target fragment when navigated using Fragment Navigation (nav graph)

currently my app has one activity with two fragments, One is HomeFragmentand the other is SearchFragment. I am using Fragment Navigation to navigate between Fragments.

In the SearchFragment, I am asking the user to provide the value that he/she wants to search. Once done, I want the app to navigate back to HomeFragment and perform the search. For that purpose, I created an Interface and build a method in SearchFragment called addListener()to set that listener.

Not to navigate from HomeFragment to SearchFragment I am using following line:

    findNavController().navigate(R.id.action_home_to_search_fragment, bundle)

This gives me no option to call a method of SearchFragment as I have no instance of that. Can anyone here help me out on this?

How to get the instance or call a method, it would be a big help.

Thanks

Upvotes: 1

Views: 1141

Answers (3)

Arpit Shukla
Arpit Shukla

Reputation: 10493

Since you are using the Navigation library, you can pass the query as an argument to HomeFragment. For this, add an argument to HomeFragment in the nav graph with a default value null. Use that query argument in HomeFragment to display the appropriate data.

In the SearchFragment, when user types the query and hits Search, instead of going back to HomeFragment via navigateUp() or popBackStack() you can move forward to HomeFragment via an action. And while going from SearchFragment to HomeFragment, remember to pop these two out of the back stack first (use popUpTo attribute in your nav graph for this) so that you have only HomeFragment in the back stack at the end.

Another approach: Probably a more straightforward way will be to use the Fragment Result API to send data from one fragment to another. You can refer docs for understanding the usage.

Upvotes: 1

Radin
Radin

Reputation: 5

You can also include Navigation Safe Args. then in nav_graph carry out an action from SearchFragment to HomeFragment, and also in nav_graph set an argument for HomeFragment with type String and default value null or just "".

Then inside the send command in SearchFragment you write the following:

val action = SearchFragmentDirection.actionSearchFragmentToHomeFragment(EditText.text.toSrting)
findNavController.navigate(action)

you accept data in HomeFragment, for this you create globally an instance of your HomeFragmentArgs:

private val args: HomeFragmentArgs by navArgs()

And transfer logic from SearchFragment to HomeFragment and insert this argument

and paste this argument(args) where you make a request or call

You can learn more about Safe Args here: https://developer.android.com/guide/navigation/navigation-pass-data

Upvotes: 0

Milad Mohammadi
Milad Mohammadi

Reputation: 54

You can use MutableLiveData and store data in a LiveData in your ViewModel or Activity as bellow:

Add this line to your Module-level build.gradle file:

dependencies {
    //...
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
}

define the variable in your ViewModel:

val searchQuery: MutableLiveData<String> = MutableLiveData()

update the data when user provides the value:

viewModel.searchQuery.postValue(query)

And you can use the value in your homeFragment:

viewModel.searchQuery.observe(viewLifecycleOwner, { query: String ->
    // Do whatever you want with query
})

Upvotes: 0

Related Questions