What is the savedInstanceState Bundle? The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.
By calling super. onCreate(savedInstanceState); , you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.
onCreate(Bundle) is called when the activity first starts up. You can use it to perform one-time initialization such as creating the user interface.
onCreate()On activity creation, the activity enters the Created state. In the onCreate() method, you perform basic application startup logic that should happen only once for the entire life of the activity.
If you save the state of the application in a bundle (typically non-persistent, dynamic data in onSaveInstanceState
), it can be passed back to onCreate
if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState
is null.
... you should use the onPause() method to write any persistent data (such as user edits) to storage. In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. 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.
source
onCreate(Bundle savedInstanceState)
you will get the Bundle
null when activity get starts first time and it will get in use when activity orientation get changed .......
http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html
Android provides another elegant way of achieving this. To achieve this, we have to override a method called onSaveInstanceState()
. Android platform allows the users to save any instance state. Instance state can be saved in the Bundle. Bundle is passed as argument to the onSaveInstanceState method.
we can load the saved instance state from the Bundle passed as argument to the onCreate
method. We can also load the saved instance state in onRestoreInstanceState
method. But I will leave that for the readers to figure out.
As Dhruv Gairola answered, you can save the state of the application by using Bundle savedInstanceState. I am trying to give a very simple example that new learners like me can understand easily.
Suppose, you have a simple fragment with a TextView and a Button. Each time you clicked the button the text changes. Now, change the orientation of you device/emulator and notice that you lost the data (means the changed data after clicking you got) and fragment starts as the first time again. By using Bundle savedInstanceState we can get rid of this. If you take a look into the life cyle of the fragment.Fragment Lifecylce you will get that a method "onSaveInstanceState" is called when the fragment is about to destroyed.
So, we can save the state means the changed text value into that bundle like this
int counter = 0;
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("value",counter);
}
After you make the orientation the "onCreate" method will be called right? so we can just do this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null){
//it is the first time the fragment is being called
counter = 0;
}else{
//not the first time so we will check SavedInstanceState bundle
counter = savedInstanceState.getInt("value",0); //here zero is the default value
}
}
Now, you won't lose your value after the orientation. The modified value always will be displayed.
onCreate(Bundle savedInstanceState) Function in Android:
When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.
When ever orientation(i.e. from horizontal to vertical or vertical to horizontal) of activity gets changed or when an Activity gets forcefully terminated by any Operating System then savedInstanceState i.e. object of Bundle Class will save the state of an Activity.
After Orientation changed then onCreate(Bundle savedInstanceState) will call and recreate the activity and load all data from savedInstanceState.
Basically Bundle class is used to stored the data of activity whenever above condition occur in app.
onCreate() is not required for apps. But the reason it is used in app is because that method is the best place to put initialization code.
You could also put your initialization code in onStart() or onResume() and when you app will load first, it will work same as in onCreate().
onCreate(Bundle)
is called when the activity first starts up. You can use it to perform one-time initialization such as creating the user interface. onCreate()
takes one parameter that is either null or some state information previously saved by the onSaveInstanceState
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With