Savedinstancestate Is Always Null in Fragment

SavedInstanceState is always null in fragment

All the problem was in that I don't declare android:id for the fragment in XML. Android needs ID or TAG to recognize stored fragment and reproduce all elements in it. So guys, remember - every instance of fragment needs unique id or tag!

Also, when setRetainInstance(true) is declared then bundle should always return null.

savedInstanceState is null when onCreateView is called in Fragment even though I overrided onSaveInstanceState

Because the Activity created after onCreateView .

You can use onActivityCreated method to restore the fragment's state here .

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);
}
Log.e("TAG", "onActivityCreated OK");
Log.e("TAG", savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE) + "");
}

Edit

Change to onViewStateRestored method,and try again .

Add log in onSaveInstanceState and onViewStateRestored ,make sure all of them work .

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
mSubtitleVisible = savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE);
}
Log.e("TAG", "onActivityCreated OK");
Log.e("TAG", "onActivityCreated " + savedInstanceState.getBoolean(SAVED_SUBTITLE_VISIBLE));
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_SUBTITLE_VISIBLE, mSubtitleVisible);
Log.e("TAG", "onSaveInstanceState OK");
Log.e("TAG", "onSaveInstanceState" + mSubtitleVisible);
}

Edit

You can save the status in onSaveInstanceState and onDestroyView.

Then,restore the state in onActivityCreated

You can do like this.

Bundle savedState;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Restore State Here
if (!restoreStateFromArguments()) {
// First Time, Initialize something here
onFirstTimeLaunched();
}
}

protected void onFirstTimeLaunched() {
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save State Here
saveStateToArguments();
}

@Override
public void onDestroyView() {
super.onDestroyView();
// Save State Here
saveStateToArguments();
}

private void saveStateToArguments() {
if (getView() != null)
savedState = saveState();
if (savedState != null) {
Bundle b = getArguments();
b.putBundle("savedState", savedState);
}
}

private boolean restoreStateFromArguments() {
Bundle b = getArguments();
savedState = b.getBundle("savedState");
if (savedState != null) {
restoreState();
return true;
}
return false;
}

// Restore Instance State Here
private void restoreState() {
if (savedState != null) {
// For Example
uSelected.setText(savedState.getString("uSelected"));
onRestoreState(savedState);
}
}

protected void onRestoreState(Bundle savedInstanceState) {
}

// Save Instance State Here
private Bundle saveState() {
Bundle state = new Bundle();
// For Example
state.putString("uSelected", uSelected.getText().toString());
onSaveState(state);
return state;
}

protected void onSaveState(Bundle outState) {
}

Fragment life cycle

Sample Image

Android - Bundle savedInstanceState is null in Fragment onCreate() and onCreateView()

savedInstanceState contains the data saved in onSaveInstanceState(), which is called before your Activity/Fragment is getting killed by the OS (to reclaim memory for example, or because of an orientation change).

Call getArguments() to get the Bundle you saved with setArguments():

order = (Order) getArguments().getSerializable("order");

Also, the recommended practice to instantiate a Fragment with arguments is via a static newInstance() method, something like this:

public static OrderDetailFragment newInstance(Order order) {
Bundle args = new Bundle();
args.putSerializable(KEY_ORDER, order);
OrderDetailFragment fragment = new OrderDetailFragment();
fragment.setArguments(args);
return fragment;
}

And then you instantiate your Fragment like this:

OrderDetailFragment orderDetailFragment = OrderDetailFragment
.newInstance(orders.get(position));

SavedInstanceState is always null even though it has an id

From the documentation on activity lifecycle:

Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.

The method onSaveInstanceState is not used for your purpose, it's used in a case like screen rotation to save UI state.
You have to switch your logic passing the values through the intent when the first Activity launches the second one and passing the values back again using onActivityResult().

In addition, I recommend you to restore your state not in onCreate() but in onRestoreInstanceState(Bundle savedInstanceState) in which you are ALWAYS sure that your savedInstanceState Bundle isn't null.



Related Topics



Leave a reply



Submit