Persevere State Information During Changes In Configuration

Overview
 
Below are some useful links to start programming of an Android Application.
  1. Android Programming - Day One
  2. Android Programming - Day Two
  3. How To Display A Dialog Windows In Android
  4. How To Display A Progress Dialog Window In Android 
  5. Intent In Android
  6. Return Data Using Intent Object In Android Applications
  7. Passing Data Using An Intent Object In Android Applications
  8. Fragments In Android Applications
  9. Adding Fragments Dynamically In Android Application
  10. Fragment Life Cycle In Android Application
  11. Interaction Between Two Fragments
  12. Calling In Built Functions in Android Application
  13. Intent Object and Intent Filters in Android Application
  14. Displaying Notifications in Android Applications
  15. User Interface in Android Applications
  16. Orientation, Anchoring, Resizing And Repositioning Of Views In Android Application
  17. Activity Behavior When Screen Orientation Changes in Android Application

Introduction

 
So far, I have already discussed in my last article Activity Behavior When Screen Orientation Changes in Android Application that changing the screen orientation destroys an activity and recreates it. In this article, you will learn about persisting state information when changes in configuration are done.
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.
  1. onPause() – This method is always fired, whenever an activity is killed or pushed into the background.

  2. 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.
  1. @Override  
  2. public void onSaveInstanceState(Bundle bundle)  
  3. {  
  4.   
  5.     //-----------Save whatever you need to persist------  
  6.   bundle.putString("EmpId","E01");  
  7.   super.onSaveInstanceState(bundle);  
  8. }  
 
 
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.
  1. @Override  
  2.  public void onRestoreInstanceState(Bundle saveInBundle)  
  3.  {  
  4.      //----------------Retrieve the information persisted earlier-----  
  5.        super.onRestoreInstanceState(saveInBundle);  
  6.        String EmpId = saveInBundle.getString("EmpId")  
  7.  }  
 
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.
  1. @Override  
  2.  public void onRetainNonConfigurationInstance()  
  3.  {  
  4.    //----- Save whatever you want here it takes in an Object type----  
  5.    return(“Welcome Back!”);  
  6.  }  
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.
  1. @Override  
  2.  public void onCreate(Bundle savedInstanceState)  
  3.  {  
  4. //----- Save whatever you want here it takes in an Object type----  
  5.   
  6.         Super.onCreate(savedInstanceState);  
  7.        setContentView(R.layout.main);  
  8.         Log.d(“StateInfo”, “onCreate”);  
  9.         String str1 = (String) getLastNonConfigurationInstance();  
  10. }  
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.


Similar Articles