Tuesday 10 September 2013

Saving data in edittext when hitting Back button

Saving data in edittext when hitting Back button

So on activity 1 I click a button that takes me to activity 2. In activity
2 I enter some data into an EditText. When I hit the back button on the
phone it takes me to activity 1 which is correct but if I hit the activity
1 button again any text that I entered into the EditText is gone. I am
sure this is because I am starting a new Intent every time I hit my button
and I think I need to be using Flags but I am not certain. Below is my
basic MainActivity1 and MainActivity2 without the code I tried that didn't
work.
MainActivity1
public class MainActivity extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
MainActivity2
public class MainActivity2 extends Activity {
EditText et1;
@Override
protected void onCreate(Bundle outState) {
super.onCreate(outState);
setContentView(R.layout.activity_main_2);
et1 = (EditText)findViewById(R.id.editText1);
}
}
}
Thank you in advance.

No comments:

Post a Comment