0

Possible Duplicate:
Android fragments setRetainInstance(true) not works (Android support library)

I wrote a simple test project, but I cant understand why I always receive savedInstanceState = null in lifecycle methods onCreate, onCreateView and onActivityCreated. I change the screen orientation, see the log, but state not saved. Tell me please where is my mistake. Thanks. The code of fragment class is:

public class TestFragment extends Fragment {

private String state = "1";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        //never works
        state = savedInstanceState.getString("state");
    }
    //always prints 1
    Toast.makeText(getActivity(), state, Toast.LENGTH_SHORT).show();
    return inflater.inflate(R.layout.fragment_layout, container, false);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("state", "2");
    Log.e("", "saved 2");
}

}

EDIT

When i try to use setRetainInstance i have no result again((( I change a state to 2 using btn1, but after changing orientation i see 1 when pressing on btn2. Help please(

public class TestFragment extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    setRetainInstance(true);
    super.onCreate(savedInstanceState);
}

private String state = "1";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_layout, container, false);

    //button for changing state
    ((Button)view.findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            state = "2";
        }
    });

    //button for showing state
    ((Button)view.findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            Toast.makeText(getActivity(), state, Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

}

Community
  • 1
  • 1
j2esu
  • 1,647
  • 1
  • 16
  • 26

1 Answers1

0

I have seen the same issue. But you can save the instate state in an other way, thanks to the method setRetainInstance(true), all your class data are saved automatically.

@Override
public void onCreate(Bundle savedInstanceState) {
    setRetainInstance(true);
    super.onCreate(savedInstanceState);
}

EDIT

Try to also add these params when you declare your activities :

    <activity
        android:name="MyActivity"
        android:configChanges="orientation|keyboardHidden|screenSize" />
Aurélien Guillard
  • 1,203
  • 8
  • 20
  • thank you for your answer! but i cant save state again( i've edited a post, please look at it if you have some time – j2esu Oct 15 '12 at 16:32
  • Thanks a lot man... by adding this line android:configChanges="orientation|keyboardHidden|screenSize" it works for me like charm :) – Zeeshan Sep 18 '14 at 12:43
  • 1
    Well, the manifest change makes that the activity has to handle configuration changes itself, i.e. disables the system's re-building / adjustments of the screen. So if you have e.g. different layouts for portrait or landscape it will not switch. Additionally, `setRetainInstance` is in this case not necessary as nothing will be re-created. – User May 17 '17 at 07:48