2

I have three fragments A,B,C In fragment A - a get some variable id, from server then i create bundle , put id in it and start fragment B, in this fragment i get id from bundle and display all information by this id on display, but when i start fragment C and then i press button back and return ti fragment B, variable id is null. How can i solve it? I think i can save id in Database,but maybe there is easier way? i understand that when i press back in fragment C, fragment B want to get information from bundle, thats why i make next:

Bundle bundle = getArguments();
if (fakeID == null) {
    fakeID = bundle.getString("fakeID");
}

Edited:

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState); 
    if (savedInstanceState == null)
    {
        Bundle bundle = getArguments();
        fakeID = bundle.getString("fakeID");
    }
    else{
        fakeID = savedInstanceState.getString("fakeID");
    }
 }
 @Override
public void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    outState.putString("fakeID", fakeID);
}

But when i return to this fragment savedInstanceState == null in this statement

if (savedInstanceState == null)
Kostya Khuta
  • 673
  • 2
  • 7
  • 21

1 Answers1

1

according to this onSaveInstanceState won't be called unless the activity itself has called it. which means if you are in the same activity changing fragments. onSavedInstanceChanged won't be called. Therefore it is null. As the link suggested you have to use either SharedPreferences or Database.

Community
  • 1
  • 1
Coderji
  • 7,655
  • 5
  • 37
  • 51