Android: Onsaveinstancestate Not Being Called from Activity

Android: onSaveInstanceState not being called from activity

Please notice that onRestoreInstanceState() is called when activity is recreated but only if:

it was killed by the OS. "Such situation happen when:

  • orientation of the device changes (your activity is destroyed and recreated)
  • there is another activity in front of yours and at some point the OS kills your activity in order to free memory (for example). Next time when you start your activity onRestoreInstanceState() will be called."

So if you are in your activity and you hit Back button on the device, your activity is finish()ed and next time you start your app it is started again (it sounds like re-created, isn't it?) but this time without saved state because you intentionally exited it when you hit Back button.

onSaveInstanceState is not getting called

onSaveInstanceState() is only called if the Activity is being killed.

I don't know what exactly you want to do in that method, but you probably should move your code to the corresponding methods of the Activity Lifecycle.

from http://developer.android.com/reference/android/app/Activity.html :

Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

Also the method description for onSaveInstanceState() describes exactly your situation:

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

onSaveInstanceState is not getting called after screen rotation

Unless your app is running on Lollipop (API21) version of Android or newer, your

public void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState);

will NOT be called as it simply does not exist on earlier versions of the platform than 21. To support pre API 21 devices you must, instead of the above, override the following method:

public void onSaveInstanceState (Bundle outState);

This will work on API 21+ as well, so you do not need to override both methods, of course (unless you know you need to deal with PersistableBundle the new one offers).

See docs.

android - manually calling onSaveInstanceState not working

No you cannot call onSaveInstanceState manually. The framework does that for you. Calling that will only update your bundle, not the framework bundle. The proper way is to override it and save your data.

onSaveInstanceState not working

public class Main extends Activity implements OnClickListener, OnKeyListener {

EditText textitem;
Button buttonadd;
ListView listitems;

ArrayList<String> ToDo;
ArrayAdapter<String> AA;
ArrayList<String> MyArrayList;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textitem = (EditText) findViewById(R.id.textitem);
buttonadd = (Button) findViewById(R.id.buttonadd);
listitems = (ListView) findViewById(R.id.listitems);

buttonadd.setOnClickListener(this);
textitem.setOnKeyListener(this);

if(savedInstanceState!=null)
{
ToDo = savedInstanceState.getStringArrayList("MyArrayList");
}
else
{
ToDo = new ArrayList<String>();
}
AA = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ToDo);
listitems.setAdapter(AA);

}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putStringArrayList("MyArrayList", ToDo);
super.onSaveInstanceState(savedInstanceState);
}

private void addItem(String item) {
if (item.length() > 0) {
this.ToDo.add(item);
this.AA.notifyDataSetChanged();
this.textitem.setText("");
}
}

public void onClick(View v) {
if (v == this.buttonadd) {
this.addItem(this.textitem.getText().toString());
}
}

public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
this.addItem(this.textitem.getText().toString());
}
return false;
}

Hope this will help you.
Vipul

onSaveInstanceState() is not gauranted to be called

I would use onPause to persist object data.
See this activity life cycle diagram
http://developer.android.com/training/basics/activity-lifecycle/starting.html

Note: you should still use onSaveInstanceState() and onRestoreInstanceState(). These are your opportunity to save and restore the state of your application when for example the screen is rotated and the app is killed and then restarted.

onSaveInstanceState() not being called when user presses home button

As it turns out, the issue was not with which Android version was being used, or how the code was written, the problem was in the manifest file. For some reason or another (copy paste, default setting), i had the tag noHistory marked as true for my activity.

<activity android:name=".MainActivity" android:noHistory="true"></activity>

I am not sure why i had that there but it was causing all sorts of problems. I switched to:

<activity android:name=".MainActivity"></activity>

Having the noHistory tag marked to true prevented onSaveInstanceState() from being called when the user pressed the home button (app goes to background) but still allowed for the state to be saved when screen orientation changes happened.

I had never encountered this behavior before and other questions that had issues with onSaveInstanceState not being called never mentioned checking the manifest file for this tag, so hopefully this helps to clarify any issues for other folks.



Related Topics



Leave a reply



Submit