This week's book giveaway is in the Spring forum.
We're giving away four copies of Java Persistence with Spring Data and Hibernate and have Cătălin Tudose on-line!
See this thread for details.
Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

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

 
Greenhorn
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.

MainActivity.java


CartOrderFragment.java



FoodListAdapter.java

 
Ranch Hand
Posts: 547
2
  • 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.
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic