This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Joe Ess
  • salvin francis

What is the best practice way to save fragment state when you have to replace many of them?

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a main activity with many fragments that get replaced on each other and there state always get lost I even tried public void onSaveInstanceState(@NonNull Bundle outState) but it still didn't work I even did it in the MainActivity to retain the instance but still nothing now I have a CartOrderFragment that I add items to it from FoodListFragment when a button is clicked, but every time I go to CartOrderFragment and come back to FoodListFragment and try to add more item by clicking on the same button before, the CartOrderFragment lose its list.

Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It seems weird that you are trying to save retrieve a fragment from the saved instance state.
Why don't add and retrieve the items list that populate the fragment instead?

What you are trying to is pass and retrieve the orderlist/foodlist but you are doing is passing and retrieving the container.  
Use the bundle for data not containers.

If the fragment you are retrieving from the bundle
is not empty here :

then it appears the fragment is not saving it's contents.

In the code below getSupportFragmentManager is probably generating a new fragment.

Try storing and retrieving the foodlist array to the bundle then repopulate the fragment that holds it in onCreate().

Hope this helps.
Police line, do not cross. Well, this tiny ad can go through:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!