Header Ads

ad728
  • Breaking News

    OnSaveInstantState Method

    In android app when ever activities changes their orientation (portal/landscape) ,they are going to loss their UI state. To avoid this, we use OnSaveInstantState Method.

    activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.blogspot.csdevbin.onsaveinstantstatemethod.MainActivity">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="58dp"
            android:hint="First Number" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText2"
            android:layout_below="@+id/editText"
            android:layout_centerHorizontal="true"
            android:hint="Second Number" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="add"
            android:onClick="add"
            android:id="@+id/button"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Result"
            android:id="@+id/textView"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/button" />
    </RelativeLayout>
    MainActivity.java
    package com.blogspot.csdevbin.onsaveinstantstatemethod;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        EditText editText1, editText2;
        TextView tv;
        int result;
    
       
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            editText1 = (EditText) findViewById(R.id.editText);
            editText2 = (EditText) findViewById(R.id.editText2);
            tv = (TextView) findViewById(R.id.textView);
    
            if (savedInstanceState != null) {
                 result = savedInstanceState.getInt("result");
                tv.setText("addition is : " + result);
            }
        }
    
        public void add(View v) {
            String s1 = editText1.getText().toString();
            String s2 = editText2.getText().toString();
    
             result = Integer.parseInt(s1) + Integer.parseInt(s2);
    
            tv.setText("addition is : " + result);
        }
    
        protected void onSavedInstantState(Bundle Outstate) {
                super.onSaveInstanceState(Outstate);
               Outstate.putInt("result",result);
        }
    }

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728