May 01, 2019

How to save an Android Activity state using save instance state

      On this blog am going to explain how to save activity state when the activity is destroyed, I have been working on the android SDK platform, and it is a little unclear how to save an application's state.


You're explicitly destroying your activity such as by pressing back. Actually, the scenario in which this savedInstanceState is used, is when Android destroys your activity for recreation. For example, If you change the language of your phone while the activity was running (and so different resources from your project need to be loaded). Another very common scenario is when you rotate your phone to the side so that the activity is recreated and displayed in landscape. You can use this technique to store instance values for your application (selections, unsaved text, etc.).


To overcome this issue you need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter, This bundle will be passed to onCreate if the process is killed and restarted.


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putBoolean("KeyBoolean", true);
  savedInstanceState.putDouble("KeyDouble", 1.9);
  savedInstanceState.putInt("KeyInt", 1);
  savedInstanceState.putString("KeyString", "Welcome back to Android");
}


The Bundle is essentially a way of storing the values as  Name-Value Pair, and it will get passed in to onCreate() and also  onRestoreInstanceState(Bundle savedInstanceState) where you would then extract the values like this,


@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  boolean myBoolean = savedInstanceState.getBoolean("KeyBoolean");
  double myDouble = savedInstanceState.getDouble("KeyDouble");
  int myInt = savedInstanceState.getInt("KeyInt");
  String myString = savedInstanceState.getString("KeyString");
}





20 comments:

  1. Wonderful post. I simply stumbled upon your weblog and wanted to mention that I have
    truly loved surfing around your blog posts.
    You can find more information regarding software services click herebest software solution in hyderabad

    ReplyDelete
  2. This is such a great resource that you are providing and you give it away for free. https://www.buyyoutubesubscribers.in/

    ReplyDelete
  3. Are you looking for Buy Vegan Food Products online Delhi, India. You have one top and tursted website that is VeggieChamp, they are accepting online order in Delhi
    veg chicken
    vegan meat
    plant based meat
    vegan fish
    vegan chicken
    vegan hot dogs
    vegan sausage

    ReplyDelete
  4. VeggeiChamp was established in India in 2008. Located in New Delhi. It is the first company to go into “Mock Meat” production in the country. Ahimsa Food is committed to producing high-quality gourmet Vegetarian food.
    mock duck
    Chicken popcorn
    Vegan Chicken
    Vegan Meat
    Veggie Burgers
    Online Food

    ReplyDelete
  5. Thank Author, For such an informative article,...Sometimes travelers get overwhelmed by the complicated procedures of visa acquisition. However, getting e-visa to Turkey from Indian is as easy as making a paper plane : ) Apply Turkey e-visa from India with the online portal and get your e-visa in 3 steps! And pay your Turkey visa cost from India online.

    ReplyDelete