Full source code in Android When there are runtime configuration changes in your Android phone, like changing the screen orientation or the device’s language, your whole app process will be destroyed and recreated from scratch and together with it, all member variables will be reset. The system already takes care of default views like the text in an EditText field or the scrolling position of a RecyclerView or ListView. But we have to restore the variables of our activity ourselves and we do this by overriding onSaveInstanceState and passing the values to the outState Bundle. After the Activity has been recreated, there are 2 places where we can get our values back: onCreate or onRestoreInstanceState, which both get passed the savedInstanceState Bundle
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
tools:context="tech.codingpoint.saveinstancestateexample.MainActivity">
<Button
android:id="@+id/button_decrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="@+id/text_view_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="0"
android:textColor="@android:color/black"
android:textSize="50sp" />
<Button
android:id="@+id/button_increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
MainActivity.java
package tech.codingpoint..saveinstancestateexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextViewCount;
private int mCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextViewCount = findViewById(R.id.text_view_count);
Button buttonDecrement = findViewById(R.id.button_decrement);
Button buttonIncrement = findViewById(R.id.button_increment);
buttonDecrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
decrement();
}
});
buttonIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
increment();
}
});
if (savedInstanceState != null) {
mCount = savedInstanceState.getInt("count");
mTextViewCount.setText(String.valueOf(mCount));
}
}
private void decrement() {
mCount--;
mTextViewCount.setText(String.valueOf(mCount));
}
private void increment() {
mCount++;
mTextViewCount.setText(String.valueOf(mCount));
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("count", mCount);
}
}