Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 573
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic