Save a Activity in Android When moved to another Activity
Possible Duplicate:
How do I save an Android application's state?
I'm currently working an application that has the following behavior : i am using 2 Edittext and there are some value which i type in it and when i press the button it will move on to the next page or activity ,But the problem is here when i return back to activity i dont find the values in edittext PLease help me .. Please let me know the code ... Thanks in Advance....
This is my code....
public class TestsampleActivity extends Activity
{
Button b1,b2;
TextView t1;
EditText e1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1= (Button)findViewById(R.id.btn1);
b2= (Button)findViewById(R.id.btn2);
t1=(TextView)findViewById(R.id.tv1);
e1=(EditText)findViewById(R.id.et1);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v){ add(); }
});
b2.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v){ del(); }
});
}
public void add()
{
String s1= e1.getText().toString();
Intent intent=new Intent(this,next.class);
intent.putExtra("name",s1);
startActivity(intent);
/* String s1= e1.getText().toString();
//t1.append(s1);
t1.setText(s1);
*/
}
public void del()
{
e1.setText("");
t1.setText("");
}
}
You must override the onSaveInstanceState(Bundle b) to save your data before loading the new activity.
Also you must restore overriding the onRestoreInstanceState and getting the values back...
you can se more information here:
Saving Android Activity state using Save Instance State
or
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
Save the text in the edits to the provided bundel in your onSaveInsanceState() method. Then in your onRestoreInstanceState() method, use the values you saved to restore the text in the EditTexts.
When returning to the previous Activity: Use the following method:
Intent intent_obj=new Intent();
String id = intent_obj.getStringExtra("id");
e1.setText(id);
This will work....!!!
链接地址: http://www.djcxy.com/p/26184.html