Home > Uncategorized > SavedInstance : onSaveInstanceState : onRestoreInstanceState

SavedInstance : onSaveInstanceState : onRestoreInstanceState

The Main.xml file, which is associated with Activity1 (SavedInstance.java)

Main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Saved Instace Example. Once you move from one Activity to other and come back, all the information on Activity1 remains available”
/>

<EditText
android:id=”@+id/et_one”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceSmall”
/>

<EditText
android:id=”@+id/et_two”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceSmall”
/>
<Button
android:id=”@+id/submit”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Submit”/>
</LinearLayout>

Main2.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Saved Instace Example. You come from other activity, we will pass some value to previous activity”
/>

<EditText
android:id=”@+id/et_one2″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceSmall”
/>

<EditText
android:id=”@+id/et_two2″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceSmall”
/>
<Button
android:id=”@+id/submit2″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Back”/>
</LinearLayout>

Activity1 (SavedInstance.java) class

package com.ssc.save.instance;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/*
* We have two activity Activity1 and Activity2
* In Activity1, have two EditText field and a Save button
* clicking on Save button, Activity2 get called and click of submit Button of Activity2,
* Activity1 get called.
* Now I want to preserve the Activity1 data, while moving to Activity2, and vice versa
*/
public class SavedInstace extends Activity {
/** Called when the activity is first created. */
Button submit;
EditText et1;
EditText et2;
Bundle savedInstanceState;
private static final String TAG = “SavedInstace”;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// this.savedInstanceState = savedInstanceState;
submit = (Button) findViewById(R.id.submit);
et1 = (EditText) findViewById(R.id.et_one);
et2 = (EditText) findViewById(R.id.et_two);
String one = getIntent().getStringExtra(“one”);
String two = getIntent().getStringExtra(“two”);
if ((one == null) || (two == null)) {
// do nothing
} else {
one = getIntent().getStringExtra(“one”);
two = getIntent().getStringExtra(“two”);
et1 = (EditText) findViewById(R.id.et_one);
et2 = (EditText) findViewById(R.id.et_two);
et1.setText(one);
et2.setText(two);
}
// if (savedInstanceState != null) {
// String one = null;
// String two = null;
// one = savedInstanceState.getString(“one”);
// two = savedInstanceState.getString(“two”);
// et1 = (EditText) findViewById(R.id.et_one);
// et2 = (EditText) findViewById(R.id.et_two);
// et1.setText(one);
// et2.setText(two);
// }

submit.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent activity2 = new Intent(SavedInstace.this,
Activity2.class);
String one = et1.getText().toString();
String two = et2.getText().toString();
activity2.putExtra(“one”, one);
activity2.putExtra(“two”, two);
startActivityForResult(activity2, 0);
}
});
}

@Override
protected void onPause() {
Log.d(TAG, “onPause”);
super.onPause();
}

@Override
protected void onResume() {
Log.d(TAG, “onResume”);
super.onResume();

}

@Override
protected void onRestart() {
Log.d(TAG, “onRestart”);
super.onRestart();
// onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
Log.d(TAG, “onSaveInstanceState”);
String one = null;
String two = null;
boolean b = false;
et1 = (EditText) findViewById(R.id.et_one);
et2 = (EditText) findViewById(R.id.et_two);
one = et1.getText().toString();
two = et2.getText().toString();

outState.putString(“one”, one);
outState.putString(“two”, two);
super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d(TAG, “onRestoreInstanceState”);
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
String one = null;
String two = null;
one = savedInstanceState.getString(“one”);
two = savedInstanceState.getString(“two”);
et1 = (EditText) findViewById(R.id.et_one);
et2 = (EditText) findViewById(R.id.et_two);
et1.setText(one);
et2.setText(two);
}

}

public class SavedInstaceHolder {

}
}

Activity2.java class

package com.ssc.save.instance;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Activity2 extends Activity {
/** Called when the activity is first created. */
Button back;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
back = (Button) findViewById(R.id.submit2);

back.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent activity2 = new Intent(Activity2.this,
SavedInstace.class);
activity2.putExtra(“one”, getIntent().getStringExtra(“one”));
activity2.putExtra(“two”, getIntent().getStringExtra(“two”));
startActivity(activity2);
}
});
}

}

Advertisements
Categories: Uncategorized
  1. December 9, 2011 at 11:00 pm

    I must say, as a good deal as I enjoyed reading what you had to say, I could not aid but shed interest following a although. Its as should you had a wonderful grasp on the subject matter, but you forgot to include your readers.

  1. May 29, 2012 at 3:38 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: