Saving state through the Bundle when you rotate the screen

Evan Bishop
3 min readApr 6, 2019

--

Processing screen orientation changes in Android is one of the not yet automated tasks, developers need to manually save data, in some cases, change the markup and adapt the interface for tablets.

To begin with, let’s see where so many problems come from:

When the device orientation changes, the system terminates our activity and starts it again with the landscape version of the markup.

Accordingly, all the data displayed in the interface and the objects of our activity, as well as streaming processes (let’s talk about this in the second part of the article) are re-created.

Helping us not to become entangled in these seemingly simple-looking things will be helped by the already-implemented InstanceState method construction (onSave / onRestore) in the system.

1. Saving data

When creating any activity, your main method has a similar look:

protected void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

setContentView (R.layout.activity_main);

}

The method signature already contains a savedInstanceState object of type Bundle and a call to a superclass.

In order to check how the default state saving works, let’s redefine the necessary methods:

@Override

protected void onRestoreInstanceState (Bundle savedInstanceState) {

super.onRestoreInstanceState (savedInstanceState);

}

@Override

protected void onSaveInstanceState (Bundle outState) {

super.onSaveInstanceState (outState);

}

Also, data recovery from forms is supported only for markup elements whose id is explicitly specified. Create an xml file, add a text field, a button and EditText to it:

<? xml version = “1.0” encoding = “utf-8”?>

<LinearLayout

xmlns: android = “http://schemas.android.com/apk/res/android"

xmlns: tools = “http://schemas.android.com/tools"

android: layout_width = “match_parent”

android: layout_height = “match_parent”

android: orientation = “vertical”

android: weightSum = “3”

android: padding = “10dp”

tools: context = “. MainActivity”>

<Textview

android: id = “@ + id / textView”

android: gravity = “center”

android: textSize = “20sp”

android: layout_weight = “1”

android: layout_width = “match_parent”

android: layout_height = “wrap_content”

android: text = “Hello World!” />

<Edittext

android: layout_width = “match_parent”

android: layout_height = “wrap_content”

android: ems = “10”

android: layout_marginTop = “10dp”

android: layout_marginBottom = “10dp”

android: layout_weight = “1”

android: inputType = “textPersonName”

android: text = “Example” />

<Button

android: id = “@ + id / button”

android: layout_weight = “1”

android: layout_width = “match_parent”

android: layout_height = “wrap_content”

android: text = “Button” />

</ LinearLayout>

By default, if you enter something in EditText and change the orientation of the device, the text will be saved, but this does not apply to the variables that we ourselves create in our activites. For example, add the function to increase the variable in our class by pressing the button

// counter variable

private int count;

@Override

protected void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

setContentView (R.layout.activity_main);

// Set the click handler and call the counter method

Button btnCount = findViewById (R.id.button);

btnCount.setOnClickListener (new View.OnClickListener () {

@Override

public void onClick (View v) {

getCount ();

}

});

}

// Increment the variable by one and print its value.

private void getCount () {

++ count;

Toast.makeText (MainActivity.this, “Our count is” + count, Toast.LENGTH_SHORT) .show ();

}

@Override

protected void onRestoreInstanceState (Bundle savedInstanceState) {

super.onRestoreInstanceState (savedInstanceState);

}

@Override

protected void onSaveInstanceState (Bundle outState) {

super.onSaveInstanceState (outState);

}

}

Now, so that the value is saved when the screen is rotated, we need to edit the methods (onSave / onRestore) of the InstanceState as follows:

// Method of recovering values

@Override

protected void onRestoreInstanceState (Bundle savedInstanceState) {

super.onRestoreInstanceState (savedInstanceState);

// Restore our variable by key

count = savedInstanceState.getInt (“Count”);

// We can also set the default value after the key, separated by commas

}

// Save the variable before completing the activity

@Override

protected void onSaveInstanceState (Bundle outState) {

super.onSaveInstanceState (outState);

// Write the variable with the key in the Bundle

outState.putInt (“Count”, count);

}

Now we ourselves save the value of our variable and the user can rotate the device as he pleases, the information will still be saved and restored.

It is important, do not use the Bundle as a permanent repository for complex or large data, for this there is a SharedPerferences or database.

--

--

Evan Bishop

Hi, I’m an android developer and want to share my notes and tutorials