I am programming a basic shop app (I am a beginner). It has a default bottom bar. In the home fragment, there is a recyclerview implemented with a product adapter to display products from a productList. When I click on a given product, using navigationcotroller it changes to a new fragment which displays more datailed info of the product. I transfer the data of the clicked product using Json, but I do not know if it is the right way? But it works...The problem is that when I click on a button in the detailed info fragment, that navigates me back to home fragment, all data dissappears. However, when I click on the bottom bar to navigate me to dashboard fragment and then back to home fragment the data remain unchanged. How is that possible? I tried to use a viewModel, but it still does not work, but maybe i am just using it wrong...Do you have any ideas how to store data of the product on a certain fragment when switching views?
I thought that when I create an instance of a viewModel and store the data there(the list of products) it would store them even when I change fragments. But it probably does not or I do not how. Thanks for your tips.
It sounds like your arguments last only as long as their originating fragment.
If both of your fragments instantiate the same viewmodel, then you are really creating two instances of the same viewmodel. Data is only held by the first instance and cannot be passed to the second.
You need to either