Android - How To Close An Activity On Button Click?
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 Activi
Solution 1:
You want to close ActivityTwo
when a button is clicked? Just use finish();
Full code for the button listener in ActivityTwo
would be:
ButtoncloseButton= (Button) findViewById(R.id.bClose);
closeButton.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO:// This function closes Activity Two// Hint: use Context's finish() method
finish();
}
});
Solution 2:
Solution 3:
Alright the best way to finish the current Activity is by using finish()
method. So inside the onClick()
of your button in the ActivityTwo
you can do this.
closeButton.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
finish();
//closes ActivityTwo
}
});
no need to make new intents for finishing current Activity. Also note that pressing back will also finish your Activity.
Solution 4:
Replace these two lines
Intent myIntent2=new Intent(getApplicationContext(),ActivityTwo.class);
ActivityTwo.this.finish(myIntent2);
with this
ActivityTwo.this.finish();
Solution 5:
Button exit= (Button) findViewById(R.id.yes);
exit.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
MainActivity.this.finish();
}
});
Post a Comment for "Android - How To Close An Activity On Button Click?"