Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

How to set back key action on Android

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...