Monday, 7 November 2011

More on onSaveInstanceState() and onRestoreInstanceState()

In the article onSaveInstanceState() and onRestoreInstanceState() described how to save and restore state in onSaveInstanceState() and onRestoreInstanceState() methods. It's another exercise, we can know more of life cycle of a activity about onSaveInstanceState() and onRestoreInstanceState().

onSaveInstanceState() and onRestoreInstanceState()

package com.exercise.AndroidSaveState;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidSaveStateActivity extends Activity {

TextView textOnPause;
TextView textOnResume;
TextView textOnRestoreInstanceState;
TextView textOnSaveInstanceState;

int cntOnPause;
int cntOnResume;
int cntOnRestoreInstanceState;
int cntOnSaveInstanceState;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textOnPause = (TextView)findViewById(R.id.onPause);
textOnResume = (TextView)findViewById(R.id.onResume);
textOnRestoreInstanceState = (TextView)findViewById(R.id.onRestoreInstanceState);
textOnSaveInstanceState = (TextView)findViewById(R.id.onSaveInstanceState);

}

@Override
protected void onPause() {
// TODO Auto-generated method stub
cntOnPause++;
super.onPause();
}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

cntOnResume++;
textOnPause.setText("cntOnPause: " + String.valueOf(cntOnPause));
textOnResume.setText("cntOnResume: " + String.valueOf(cntOnResume));
textOnRestoreInstanceState.setText("cntOnRestoreInstanceState: " + String.valueOf(cntOnRestoreInstanceState));
textOnSaveInstanceState.setText("cntOnSaveInstanceState: " + String.valueOf(cntOnSaveInstanceState));
}

@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
cntOnSaveInstanceState++;
outState.putInt("CNT_OnPause", cntOnPause);
outState.putInt("CNT_OnResume", cntOnResume);
outState.putInt("CNT_OnRestoreInstanceState", cntOnRestoreInstanceState);
outState.putInt("CNT_OnSaveInstanceState", cntOnSaveInstanceState);


super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub

if (savedInstanceState != null){
if(savedInstanceState.containsKey("CNT_OnPause")){
cntOnPause = savedInstanceState.getInt("CNT_OnPause");
}

if(savedInstanceState.containsKey("CNT_OnResume")){
cntOnResume = savedInstanceState.getInt("CNT_OnResume");
}

if(savedInstanceState.containsKey("CNT_OnRestoreInstanceState")){
cntOnRestoreInstanceState = savedInstanceState.getInt("CNT_OnRestoreInstanceState");
}

if(savedInstanceState.containsKey("CNT_OnSaveInstanceState")){
cntOnSaveInstanceState = savedInstanceState.getInt("CNT_OnSaveInstanceState");
}
}
cntOnRestoreInstanceState++;

super.onRestoreInstanceState(savedInstanceState);
}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<TextView
android:id="@+id/onPause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/onResume"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/onRestoreInstanceState"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/onSaveInstanceState"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

No comments:

Post a Comment