How to set back key action on Android

326 views Asked by At

I'm working on an Android application that starts from an activity that serves as the menu screen (I'll call this activity the menu activity).

There is a button that moves the app to another activity where the user can enter their name (I'll call this activity the name activity), and then the app returns to the menu activity.

After entering my name, (I have now gone like this: menu - name - menu) when I press the back button, it goes back to the name activity, with the name still present in the EditText field.

I'd like to know if there's some way to change the back key's functionality to avoid going to the name activity, and instead return to the main activity (The hierarchy is - main - menu - name).

Also, is there some way I can prevent the name from being retained in the EditText?

Thanks in advance.

2

There are 2 answers

3
Deegriz On BEST ANSWER

I'm a bit confused by the hierarchy but either way this is what you're looking for if you want to clear any previous activities: Clearing Activity Backstack

Otherwise call finish() on the Activity you want to close before changing. That will clear the EditText

3
moffeltje On

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 (Ais 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);.