Overview
Below are some useful links to start programming of an Android Application.
Introduction
It is most important to remember that when an activity is recreated, its current state may be lost. When an activity is killed, it will fire both of the two methods given below.
- onPause() – This method is always fired, whenever an activity is killed or pushed into the background.
- onSaveInstanceState() – This method is also fired, whenever an activity is about to be killed or put into the background (same as the onPause() method). However, unlike the onPause() method, the onSaveInstanceState() method is not fired, when an activity is being unloaded from the stack (when the user pressed the back button) because there is no feed to restore its state later.
Here, one point is clear: that to preserve the state of activity, you can implement onPause() method and then, use your own way of the implementation to preserve the state of your activity by using some techniques, like a database, restful API, internal or external file storage, etc.
Explanation
If you want to persist the state of an activity so that it can be restored later, when the activity is recreated (when the device changes orientation), a much simpler way is to implement the onSaveInstance() method, as it provides a Bundle object as an argument so that you can use it to save your activity’s state. The code given below shows that you can save the string ID into the Bundle object during the onSaveInstance() method.
- @Override
- public void onSaveInstanceState(Bundle bundle)
- {
-
-
- bundle.putString("EmpId","E01");
- super.onSaveInstanceState(bundle);
- }
When activity is recreated, the onCreate() method is first fired, followed by the onRestoreInstanceState() method, which enables you to retrieve the state, which you saved previously in the onSavedInstanceState() method through the Bundle object in the arguments.
- @Override
- public void onRestoreInstanceState(Bundle saveInBundle)
- {
-
- super.onRestoreInstanceState(saveInBundle);
- String EmpId = saveInBundle.getString("EmpId")
- }
You can use the onSaveInstanceState() method to save state information. One limitation is that you can only save your state information in a Bundle object. If you need to save more complex data structures, then this is not an ideal solution.
There is another method, which you can use is the onRetainNonConfigurationInstance() method. This method is fired when an activity is about to be destroyed due to a configuration change (such as a change in screen orientation, keyboard availability, etc). You can save your current data by returning it in this method, as shown below.
- @Override
- public void onRetainNonConfigurationInstance()
- {
-
- return(“Welcome Back!”);
- }
The method given above returns an Object type, which allows you to return any data type. To extract the saved data, you can extract it in the onCreate() method, using the getLastNonConfigurationInstance() method.
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
-
-
- Super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Log.d(“StateInfo”, “onCreate”);
- String str1 = (String) getLastNonConfigurationInstance();
- }
The methods onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() are useful, when you need to persist some data momentarily, such as when you have downloaded data from a Web Service and the user changes the screen orientation. In this case, saving the data using the preceding two methods is much more efficient than downloading the data again.
Conclusion
In the article given above, you learned about persisting the state information, when the changes in the configuration occur. In the next article, I will explain how to detect orientation changes and controlling the orientation of the activity.