I use ListFragment
setRetainInstance (true); does not work, I do not know why
savedInstanceState == null
my sources
public class Fragment_Left extends ListFragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(LOG_TAG, "Fragment1 onAttach");
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "Fragment1 onCreate");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
getListAdapter().getItem(position);
}
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
Log.i(LOG_TAG, "onSaveInstanceState()");
}
}
public class Fragment_Left extends ListFragment {
private ArrayList<Menu_item> menu_list; .....
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
menu_list = new ArrayList<Menu_item>();......
but After I turned Screen menu_list is null, or I did something wrong?
I am assuming that by “store an array in a fragment” that you mean “retain an array used by a fragment across configuration changes”. If so:
Option #1: Put it in a data member of the fragment, and call
setRetainInstance(true)on that fragment.Option #2: If the array is of a data type that is supported by
Bundle, overrideonSaveInstanceState(), put the data in theBundle, and retrieve that data in other methods in the new fragment instance created after the configuration change (e.g.,onCreateView()).Option #3: If the array really represents the data model of your app, hold it in a central persistent spot that the old and new fragment instances can access, such as a database or a file.