Skip to content

Instantly share code, notes, and snippets.

@noaht11
Last active July 6, 2018 12:52
Show Gist options
  • Save noaht11/ec2375e2f8224f65341f to your computer and use it in GitHub Desktop.
Save noaht11/ec2375e2f8224f65341f to your computer and use it in GitHub Desktop.
Where to restore state in a Fragment
private static final String STATE_ID = "id";
private static final String STATE_ITEMS = "items";
private long mId;
private ArrayList<Item> mItems;
private ListView mListView;
private ArrayAdapter<Item> mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
// Restore some state before we've even inflated our own layout
// This could be generic things like an ID that our Fragment represents
mId = savedInstanceState.getLong(STATE_ID, 0);
}
}
@Override
public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.my_fragment, container, false);
// Get references to some views
mListView = (ListView) root.findViewById(R.id.list_view);
if (savedInstanceState != null) {
// Restore some state right after inflating our layout
// Note: Our views haven't had their states restored yet
...
}
}
@SuppressWarnings("unchecked")
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
// Restore some state that needs to happen after the Activity was created
//
// Note #1: Our views haven't had their states restored yet
// This could be a good place to restore a ListView's contents (and it's your last
// opportunity if you want your scroll position to be restored properly)
//
// Note #2:
// The following line will cause an unchecked type cast compiler warning
// It's impossible to actually check the type because of Java's type erasure:
// At runtime all generic types become Object
// So the best you can do is add the @SuppressWarnings("unchecked") annotation
// and understand that you must make sure to not use a different type anywhere
mItems = (ArrayList<Item>) savedInstanceState.getSerializable(STATE_ITEMS);
} else {
mItems = new ArrayList<>();
}
mAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, mItems);
mListView.setAdapter(mAdapter);
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState != null) {
// Restore some state that needs to happen after our own views have had
// their state restored
// DON'T try to restore ListViews here because their scroll position will
// not be restored properly
...
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_COUNTER, mCounter);
outState.putSerializable(STATE_ITEMS, mItems);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment