How to Implement OnSavedInstanceState in Android?

Android provides saveinstancestate this work on UI. It works like this when changing the orientation of the phone while using this method. At the time orientation of all components is restored but when using saveinstancstate, it can not be restored in the application. The saveinstacestate is a reference to the bundle object that is passed into the oncreate method in Android application.


Step-By-Step Implementation

Step 1:Create a new project in Android or you can use an already created project. 


or



Step 2: Create an XML file named Acitivity_main.xml.

 


Step 3: Acitivity_main.xml file continent Design of screen like button, text view, etc.

 <?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:gravity="center_horizontal"

android:orientation="vertical"

android:padding="16dp"

tools:context=".MainActivity">

  <TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textAlignment="center"

android:textSize="22sp"

android:text="Candidroot solutions"/>

   <EditText

     android:id="@+id/edit_text"

     android:layout_marginTop="10dp"

     android:layout_width="match_parent"

     android:layout_height="wrap_content"

     android:background="@android:drawable/editbox_background"

     android:hint="Enter text"

     android:padding="12dp" />

 

    <RadioGroup

     android:id="@+id/radio_group"

     android:layout_width="wrap_content"

     android:layout_height="wrap_content"

     android:layout_marginTop="16dp"

     android:orientation="horizontal">

     <RadioButton

         android:id="@+id/rb_true"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:padding="12dp"

         android:text="Male"

         android:textSize="24sp" />

      <RadioButton

         android:id="@+id/rb_false"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:padding="12dp"

         android:text="Female"

         android:textSize="24sp" />

 </RadioGroup>

 </LinearLayout>

Step 4: Create ActivityMain.java or java in your project inside (com.my application).

 import android.os.Bundle;

 import android.text.Editable;

 import android.text.TextWatcher;

 import android.widget.EditText;

 import android.widget.RadioGroup;

 import android.widget.Toast;

 import androidx.annotation.NonNull;

 import androidx.appcompat.app.AppCompatActivity;

 public class MainActivity extends AppCompatActivity {

// initialize variables

private EditText editText;

private RadioGroup radioGroup;

private String string;

private boolean testBoolean;

private  int anInt;

 @Override

 protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_main);

     // assign variables

     editText = findViewById(R.id.edit_text);

     radioGroup = findViewById(R.id.radio_group);

     editText.addTextChangedListener(new TextWatcher() {

         @Override

         public void beforeTextChanged(CharSequence s, int start, int count, int after) {

         }

         @Override

         public void onTextChanged(CharSequence s, int start, int before, int count) {

             // get string value

             string = String.valueOf(s);

         }

         @Override

         public void afterTextChanged(Editable s) {

         }

     });

     radioGroup.setOnCheckedChangeListener((group, checkedId) -> {

         // get boolean value

         testBoolean = checkedId == R.id.rb_true;

     });

}

  @Override

  protected void onSaveInstanceState(@NonNull Bundle outState) {

     // put string value

     outState.putString("string_value", string);

     // put boolean value

     outState.putBoolean("boolean_value", testBoolean);

     // Put int value

     outState.putInt("int_value", anInt);

     super.onSaveInstanceState(outState);

}

 @Override

 protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {

     string = savedInstanceState.getString("string_value");

     testBoolean = savedInstanceState.getBoolean("boolean_value");

     anInt = savedInstanceState.getInt("int_value");

     // display toast

     Toast.makeText(getApplicationContext(), string + " - " + testBoolean + " - " + anInt, Toast.LENGTH_SHORT).show();

     super.onRestoreInstanceState(savedInstanceState);

 }

 }

Step 5: Output of above example


Happy coding!



















365Bloggy May 20, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive