Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - How to close an activity on button click?

Tags:

I want a button click to close an Activity. I am new to intents and and a little confused. This is ActivityOne which keeps a track of life cycle.
On a button press, it opens ActivityTwo and puts ActivityOne in background.
This works fine when I used this intent in my onClickListener:

Intent myIntent = new Intent(ActivityOne.this, ActivityTwo.class); ActivityOne.this.startActivity(myIntent); 

In ActivityTwo I need to use intents and finish() method to close the activity. I tried to Google it and I thought this might work:

Intent myIntent2 = new Intent(getApplicationContext(), ActivityTwo.class); ActivityTwo.this.finish(myIntent2); 

It didn't work the full code is below:

public class ActivityOne extends Activity {      // ...      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_one);          // ...          Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);          launchActivityTwoButton.setOnClickListener(new OnClickListener() {              @Override             public void onClick(View v) {                 // TODO:                 // Launch Activity Two                 // Hint: use Context's startActivity() method                  // Create an intent stating which Activity you would like to start                 Intent myIntent = new Intent(ActivityOne.this, ActivityTwo.class);                  // Launch the Activity using the intent                 ActivityOne.this.startActivity(myIntent);                            }         });          // ...     } } 

Activity 2:

public class ActivityTwo extends Activity {          // ...      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_two);          // ...          Button closeButton = (Button) findViewById(R.id.bClose);          closeButton.setOnClickListener(new OnClickListener() {              @Override             public void onClick(View v) {                  // TODO:                 // This function closes Activity Two                 // Hint: use Context's finish() method                 Intent myIntent2 = new Intent(getApplicationContext(), ActivityTwo.class);                 ActivityTwo.this.finish(myIntent2);             }         });         // ...     } } 
like image 747
Arjun Krishnan Avatar asked Jan 31 '14 06:01

Arjun Krishnan


People also ask

How do you end an activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How you can get a response back from an activity in Android?

You must call the second activity using the startActivityForResult method. In your second activity, when it is finished, you can execute the setResult method where basically you put the result information. Then, on your first activity, you override the onActivityResult method.


1 Answers

You want to close ActivityTwo when a button is clicked? Just use finish();

Full code for the button listener in ActivityTwo would be:

Button closeButton = (Button) findViewById(R.id.bClose);  closeButton.setOnClickListener(new OnClickListener() {      @Override     public void onClick(View v) {          // TODO:         // This function closes Activity Two         // Hint: use Context's finish() method         finish();     } }); 
like image 107
Michael Yaworski Avatar answered Sep 20 '22 14:09

Michael Yaworski