Search code examples
javaandroidbuttondestroy

The difference between implementation of back and cancelbutton?


I m a newbie an trying to learn Java/Android-programming.

I m doing an app for Android in Eclipse and created some buttons. I have a back and a cancel button.

Example:

I have a EditText there you can write in your name. If you write yourname and press the backbutton, then u will go back to the previous Activity, but if you go to the same Activity, then you will still see the name that you wrote in the EditText.

But if you press the cancelbutton, you will go back to the previous Activity, but when you come back, yourname will be empty. I will "kill" or "stop" the Activity.

This is the code I use for the Backbutton, what would you use for the Cancel Button? Thank YOU.

public void onClick(View v) {
    switch(v.getId()){
    case R.id.buttonBack:
        Intent intent = new Intent (AllActivity.this, MenuActivity.class);
        startActivity(intent);
        break;

Solution

  • For the cancel button you can use the below method, this will kill the activity.

     finish()
    

    so in your code it will look something like this:

     public void onClick(View v) {
         switch(v.getId()){
         case R.id.cancel:
             finish();
             break;