Reputation: 1371
We have and Android App that contains two Activities MainActivity and PageTwo. In the MainActivity we have an EditText widget set to accept number input only and a Button that goes to the next activity with the aid of an Intent. On the second activity PageTwo we have a Button that returns the user to the MainActivity with an Intent. When we enter a value in the EditText field and make the trip from the MainActivity to PageTwo activity and back the value is seemingly erased unless you use the device (emulator) back button. I am trying to maintain the Activity State of this one EditText variable
code snippets
EditText ETage;
int vAGE;
static final String V_AGE = "vAGE"; //KeyValue Pair
@Override
public void onSaveInstanceState(Bundle outState){
outState.putInt(V_AGE,vAGE);
super.onSaveInstanceState(outState);
}
//@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
vAGE = savedInstanceState.getInt(V_AGE);
ETage.setText(String.valueOf(vAGE));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//if(savedInstanceState != null){
//vAGE = savedInstanceState.getChar(V_AGE);
//}
setContentView(R.layout.activity_main);
ETage = (EditText)findViewById(R.id.editTextAge);
Upvotes: 0
Views: 177
Reputation: 5534
if you want to go back to previous Activity why you are calling intent? just simply write onBackPressed();
inside onClick
Example
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
or if you really wanted to use intent you can always use finish()
.
Example
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(PageTwo.this, MainActivity.class);
startActivity(i);
finish();
}
});
Upvotes: 1
Reputation: 725
From what I understand from your question you can do like this in second page:
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
}
Upvotes: 0
Reputation: 5312
In PageTwo
second activity don't return to MainActivity
with an intent. Simply, do following in button click:
super.onBackPressed(); // this instead of intent to start MainActivity
Upvotes: 0