In your name activity, you have a button to submit the data fields, right?
And you probably have something there like:
Intent in = new Intent(this, menu_activity.class);
startActivity(in);
You should remove that lines and replace it with finish();
So you lets assume you have the following:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Save the data to whatever you would like (database, variable, etc.)
finish();
}
});
The reason of this is because when you create a new intent, it gets placed on the activity stack, (while the current activity 'remains' on the stack at his current state but is just not visible).
For example (A
is your menu activity, B
is name):
startIntentA(); ---> stack is: A (activity A is now visible)
startIntentB(); ---> stack is: AB (activity B is now visible)
startIntentA(); ---> stack is: ABA (activity A is now visible)
So when you press the back button, the current activity is destroyed and it goes back to the most recently opened activity on the stack:
stack is now ABA
backButton(); ---> A gets destroyed. Stack is: AB (activity B is now visible)
backButton(); ---> B gets destroyed. Stack is: A (activity A is now visible)
So when you don't start a new intent at the button on activity name
, there is no activity's to go back to:
startIntentA(); ---> stack is: A (activity A is now visible)
startIntentB(); ---> stack is: AB (activity B is now visible)
finish(); ---> stack is: A (the original activity is now visible)
When you would like to save the user input even if they pressed the back button, you can override the onBackPressed()
method:
@Override
public void onBackPressed() {
// Save the data to whatever you would like (database, variable, etc.)
finish();
}
Note: you can disallow canceling the current activity with the back button by setting setCancelable(false);
.