Activity state and onSaveInstanceState()
Activity state and ejection from
system’s memory:
The
basic need to kill an activity by the system is to free up RAM in the system
and the likelihood of the system to kill process totally, depend on the state of the process at that time. Then,
Process state, in turn, depends on the state of the activity running in the process (means the callback methods that
you have covered earlier).
By the table below
one can easily understand the Relationship
between process lifecycle and activity state.
Note: The system not kills an activity
directly to free up memory or space. Instead, it kills the process in which the
activity is running. It destroys not only the activity but everything else
running in the process, as well.
What is onSaveInstanceState() ? Why we use it?
When the activity
stopped, the system calls the onSaveInstanceState() method to check what the
system have to do in that situation. If the system don’t found that method or the
method is null, it means there is no data present which system have to be
restored. Then, the data that was present before app is stopped is destroyed
forever.
It is used
because the system destroys the activity by default when the configuration
changes temporarily (means when the orientation is changed or when the app is
not focused and is on pause state) this causes wipe of UI state stored in
activity instance.
So, it’s necessary to use onSaveInstanceState() to correctly
match users expectations with system behavior(to save that UI state).
It is collection
of key-value pairs stored in a Bundle
(you will see this in onCreate method).
Bundle- It is by default instance state to save
information about each view object in activity layout such as EditText,
TextView and sonon.
To preserve more than a very small amount of data we
use onSaveInstanceState() .
The saved data that the system uses to restore the
previous state is called the instance state.
How this all works?
System creates a new instance of that activity.
(using a set of saved
data in onSaveInstanceState())
// Simple code
to use SavedInstance state method in Oncreate method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
}
else {
// Probably initialize members with default values for a new instance
}
// Probably initialize members with default values for a new instance
}
// ...
if
(savedInstanceState != null) :- If it is null, then the system is
creating a new instance of the activity, instead of restoring a previous one
that was destroyed.
// onSaveInstanceState()
method
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
public void onSaveInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
//code
}
}
Don't forget to subscribe my blog for all latest updates and for any query feel free to comment .
Good one.
ReplyDeleteVery good
ReplyDeleteAwesome
ReplyDelete👍👍👍
ReplyDelete👍👍 great work
ReplyDelete