-3

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.

  • I'd appreciate if the downvoter would tell me why he/she downvoted. – Rohil Verma Jun 19 '15 at 17:52
  • Probably the user downvoted because you have no code in your question. It's a good practice in the website to add some code to help understand the context of your problem. – Deegriz Jun 19 '15 at 17:54
  • What code would I insert here? Seems (to me) like this question is independent of the code. – Rohil Verma Jun 19 '15 at 17:58
  • It could help to see exactly how you are starting each new Activity but hey, I'm not judging, my first question had exactly the same problem. Welcome :) – Deegriz Jun 19 '15 at 18:01
  • 3
    Without code we get the impression that you haven't tried the basics explained in a large number of Android tutorials (handling activity events and activity stack specifically). We all understand what you are trying to do, we just haven't seen any effort on your part to actually do it. Showing us what you did, we can help solve your problem instead of directly solving it for you, that is how you learn :) – zgc7009 Jun 19 '15 at 18:21
  • @zgc7009 I see. Thanks for your help, I don't mind editing my questions to conform to SO's policies, its just that I couldn't see what kind of code I could use here. I'm a novice developer, clearly. Thanks for all your help everyone! – Rohil Verma Jun 21 '15 at 17:10

2 Answers2

2

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

moffeltje
  • 4,521
  • 4
  • 33
  • 57
  • I think I understand. So does finish() also send it back up to the previous activity (here, the menu_activity)? – Rohil Verma Jun 19 '15 at 18:05
  • @Yes, `finish()` will act the same as a back press. – moffeltje Jun 19 '15 at 18:08
  • In this case you want: menu starts name, name is filled in, once the user completes by pushing a button (say save) - your code calls finish, which will pop name off the activity stack (pause/stop/destroy). You can grab the text data either when responding to the user clicking a button, or in the activity life cycle methods like pause (and have back button behavior same) – Finn K Jun 19 '15 at 18:11
0

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

Deegriz
  • 1,935
  • 1
  • 18
  • 31
  • Thanks a lot! So finish() destroys the activity its used on right? – Rohil Verma Jun 19 '15 at 18:06
  • Exactly. Just be sure to call it AFTER you started the Activity you're going to, like so: Intent intent = new Intent(CurrentActivity.this, ActivityYouAreGoingTo.class); startActivity(intent); finish(); – Deegriz Jun 19 '15 at 18:09
  • 1
    I dont think this is the optimal route, but not seeing the code hard to know for sure. Activity Menu starts Activity Name (using intent/startActivity), once name text field is filled in, and user wants to return to Activity Menu, then all that is needed is finish() or back button push. Activity Name should not need intent/startActivity - just finish. – Finn K Jun 19 '15 at 18:23