I have 2 Activities. First Activity
has EditText
and Next Button
. Second Activity
has one Back Button
. In First Activity
I entered some texts in EditText
and I hit Next Button
. Now I'm in SecondActivity
, If I press Back Button
it will return to First Activity
clearing the EditText
fields. I want that EditText
field to remain same. How to achieve these steps?
FirstActivity:
public class HomeActivity extends Activity {
EditText text;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
text = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(HomeActivity.this,Second.class);
startActivity(i);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
CharSequence sequence = text.getText();
outState.putCharSequence("savedText", sequence);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
CharSequence sequence = savedInstanceState.getCharSequence("savedText");
text.setText(sequence);
}
}
SecondActivity:
public class Second extends Activity {
Button b;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
b = (Button) findViewById(R.id.button_back);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Second.this, HomeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
}
}