Android Save And Restore Instance On Configuration Change

A Scenario in which activity will be destroyed and recreated each time when the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity due to the configuration change the data which is available in portrait mode gets lost in landscape and vice-verse , to save this data during configuration change android framework provides two important methods lets see how to use them with example .



XML Layout


file : activity_main.xml
<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:padding="20dp"
    tools:context="com.tutorialsbuzz.saveinstance.MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="btnClick"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textSize="30sp"
        android:textStyle="bold" />

</LinearLayout>

onSaveInstanceState() : The system calls this method when the user is leaving your activity , and the data are store into bundle object .

onRestoreInstanceState() : The System calls this method when the activity is recreated by passing the bundle object which was saved during onSaveInstanceState() .

Activity


file : MainActivity.java
package com.tutorialsbuzz.saveinstance;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    EditText editText;
    TextView textView;

    String data = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText1);
        textView = (TextView) findViewById(R.id.textView1);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("key", data);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        data = savedInstanceState.getString("key");
        textView.setText(data);
    }

    public void btnClick(View view) {
        data = editText.getText().toString();
        editText.setText("");
        textView.setText(data);
    }

}


when you run the application and set the textview with the string from editext box onclick of button , after that rotate the device in landscape and portrait mode . and you will see the data is saved between configuration change .

No comments:

Post a Comment