0

Before I begin, I have read this, this and some more articles online. I am still unable to find a right way to do it.

So I have just one activity in my App, and 6 fragments. First one is a ListFragment which loads a list from a SQLite table. When user taps on a row in this list, I do 2 things:

1) Get an int from that row through a listener, and pass it back to the parent activity which stores it as a class variable using a simple setter method.
2) Replace this ListFragment with another simple Fragment. This new Fragment uses a simple getter() on that class variable to retrieve some information from a different table, and show all the details to the user.

So far so good. Now if I am on this details Fragment, and I change the screen orientation, the activity state is not reloaded (as I am checking if savedInstanceState is null in the onCreate()), but however, the class variables lose their value, and my app crashes.

Basically I am trying to pass data from the ListFragment to the details Fragment. I am doing it through the activity, which is causing a problem. As per Android Documentation:

All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

There is no specific code which is giving me trouble, so didn't post any.

Community
  • 1
  • 1
rgamber
  • 5,749
  • 10
  • 55
  • 99

1 Answers1

0

The onSaveInstanceState and onRestoreInstanceState is only used to save and restore per-instance state of an activity in case your activity is destroyed by OS (for example, to free the memory or in order to recreate it when the device orientation was changed). So you can save your variable in onSaveInstanceState and get them back using onRestoreInstanceState.

For your next question, I think this article will help you. Also answer by Gene for this question will help you.

Community
  • 1
  • 1
Zusee Weekin
  • 1,348
  • 2
  • 18
  • 41