Issue
I have 2 fragment: fragment1
and fragment2
I'm using Navigation
to navigate fragment1
to fragment2
:
val action = Fragment1Directions.actionFragment1ToFragment2()
navController.navigate(action)
My fragment1
contain RecyclerView
with data change. When I clicked to item in fragment1
's RecyclerView
will open fragment2
. And when I press back button I back to fragment1
with loss data.
I want to keep my data when move to fragment2
and back to fragment1
(fragment2
like popup in web). How can I do it.
Update:
I used View Model
to keep LiveData
. but when I back from fragment2
to fragment1
, fragment1
reloaded. How can I open fragment2
and hold fragment1
(temp open fragment2
). I'm using Navigation
Solution
I found the solution:
- use
View Model
to keepLiveData
- use
Dialog destinations
for fragment2. Visit https://gist.github.com/utkukutlu/bd2e37253e7184179d923321e3ea72c1 for more detail withDialog destinations
Learned experience:
- Navigation fragment only replace not have add. I want Navigation act like add so I use
Dialog destinations
- I use
Dialog destinations
so I have to useView Model
to keepLiveData
when I update my data infragment1
Answered By - An Dương
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.