Post Top Ad

Post Top Ad

Saturday 8 July 2017

Save instance state android


1.activity_main.xml:

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.kiran.lesson11_savedinstancestate.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Name"
        android:ems="10"
        android:id="@+id/count_et" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/count_tv"
        android:textSize="18sp" />

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="counting"
        android:id="@+id/count" />
</LinearLayout>


2.ActivityMain..java:

package com.example.kiran.lesson11_savedinstancestate;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private String tag="Kiran_Main";
    private TextView mCountTv;
    int count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCountTv = (TextView) findViewById(R.id.count_tv);
        if(savedInstanceState!=null){
            count= savedInstanceState.getInt("mycount");
        }else{
            count=1;
        }
        Log.i(tag,"onCreate");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        count = savedInstanceState.getInt("mycount");
        mCountTv.setText(String.valueOf(count));
        Log.i(tag,"onRestoreInstanceState");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(tag,"onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(tag,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(tag,"onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(tag,"onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(tag,"onRestart");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("mycount",count);
        Log.i(tag,"onSavedInstate");
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(tag,"onDestroy");
    }

    public void counting(View view) {
        count++;
        mCountTv.setText(String.valueOf(count));
    }
}

No comments:

Post a Comment