Posts

Showing posts from April, 2015

Restoring activity state after changing configuration

Following are the techniques to save data so that activity can resume from where it had destroyed. 1. onSaveInstanceState() You can save your serializable data in a bundle and android will save this bundle for you. The same bundle will be returned to you on on in onCreate() and onRestoreInstanceState() callback methods. onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart(). These methods are only called when needed, that is when an activity has been destroyed (because of lack of resources) and needs to be recreated by the system. Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed. e.g. @Override protected void onCreate(Bundle savedInstanceState) { ...