Fragment savedInstanceState is always null (Android support lib)

Posted by Evgeny Egorov on Stack Overflow See other posts from Stack Overflow or by Evgeny Egorov
Published on 2012-10-15T15:27:51Z Indexed on 2012/10/15 15:37 UTC
Read the original article Hit count: 176

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");
}

}

© Stack Overflow or respective owner

Related posts about android

Related posts about android-fragments