finish()destroy an activity and you won't be able to access it until you re-create it.
onBackPressed()returns to the Activity or Fragment before the one you are in at the moment, it all depends on how you have programmed it.
For example, if when you change the activity you destroy the previous one with finish(), when doing so, onBackPressed()it will not find the previous one and it will close the application, which may give you the feeling that it is a finish()but it is not, it has only put the app in background (I think this is why you may have the confusion).
Activity A -> Activity B finish()-> Activity C
In this case if you are in Activity C and you do a you onBackPressed()will return to Activity A.
Activity B finish()-> Activity C
In this case, if you are in Activity C and do a onBackPressed()leave the app in the background
It is a function to end the activity, removing it from the activity stack.
Navigating between activities naturally
A->B->Cwhen pressing backC->B->A
If it is used finish()in any activity
A->B(finish)->Cpress backC->A
A practical case would be a splashcreen, which when its load is finished, passes to another activity, but eliminating it from the stack, so if the user performs the action to go back, it will exit the app.
The back button defaults to calling finish()but if you want to override this method, add code before it returns and terminates one Activityyou must create the onBackPressed().
There is also the way to identify when the back button is pressed
@Override
public void onBackPressed() {
super.onBackPressed();
//codigo adicional
this.finish();
}
EDITION
When overriding the method onBackPressed()the user should know why he overwrites it, if he wants to return to the Activityprevious one without making any changes it does not make sense. However, I will give an example. If we want to modify the back event and warn the user "Are you sure to exit" it is advised to write this method AND tell the method that it has to return to the Activityprevious one or to the one the user wants.
I quote @FabioVenturiPastor
For example, if when changing activities you close the previous one with finish(), when doing onBackPressed() it will not find the previous one and the application will close, which may give you the feeling that it is a finish() but it is not. , it has only put the app in the background (I think this is why you may have the confusion).
As explained above, if the user overrides the method and doesn't do any additional code and doesn't specify that it activityshould return, it doesn't make any sense.
finish() : when this method is called the Activity is closed and destroyed. This method can be called wherever it is needed, for example triggered by interacting with some element in the UI or by performing some action in our application.
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//llama finish() para cerrar Activity.
finish();
}
});
onBackPressed()
onBackPressed() : When you implement this method unlike finish() it is called exclusively when you click the "back" button and you don't need to call finish() explicitly within it, this is if you callsuper.onBackPressed()it because it internally callsfinish().
@Override
public void onBackPressed() {
//Si llamas super.onBackPressed(), esto internamente ejecuta finish().
super.onBackPressed();
}
if you don't call super.onBackPressed()and want to terminate the Activity, you would have to call finish():
@Override
public void onBackPressed() {
finish();
}
Something important is that both methods, being in an Activity and having an empty back stack , can close the application if onBackPressed() calls finish() .
As a summary, finish() ends the Activity and destroys it, while onBackPressed() can detect when we activate the "back" button, it detects the "back" event and internally executes finish().
The finish() method will terminate the activity and the onBackPressed() method will be executed when the user presses the back softkey. To complement and since there is not much information about these methods for fragments, which is what I work with. If we want to finish the activity in a fragment it will be: "It is in a fragment"
@Override
public void onClick(View v){
getActivity.finish()
}
If you work with fragments and you want an alert to appear when pressing the back button that says whether or not you want to exit the application, the code must be done in the main activity where all the fragment calls began. "it is in the main_activity":
@Override
public void onBackPressed() {
AlertDialog.Builder mensaje=new AlertDialog.Builder(this);
mensaje.setTitle("¿Desea Salir de la Aplicacion?");
mensaje.setCancelable(false);
mensaje.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
mensaje.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
mensaje.show();
}
finish()
destroy an activity and you won't be able to access it until you re-create it.onBackPressed()
returns to the Activity or Fragment before the one you are in at the moment, it all depends on how you have programmed it.For example, if when you change the activity you destroy the previous one with
finish()
, when doing so,onBackPressed()
it will not find the previous one and it will close the application, which may give you the feeling that it is afinish()
but it is not, it has only put the app in background (I think this is why you may have the confusion).Activity A -> Activity B
finish()
-> Activity CIn this case if you are in Activity C and you do a you
onBackPressed()
will return to Activity A.Activity B
finish()
-> Activity CIn this case, if you are in Activity C and do a
onBackPressed()
leave the app in the backgroundfinish()
It is a function to end the activity, removing it from the activity stack.
Navigating between activities naturally
A->B->C
when pressing backC->B->A
If it is used
finish()
in any activityA->B(finish)->C
press backC->A
A practical case would be a splashcreen, which when its load is finished, passes to another activity, but eliminating it from the stack, so if the user performs the action to go back, it will exit the app.
SplasCreanActivity.java
OnBackPressed()
It is to detect the back event, when the user performs the action of going back, using the physical button, the virtual button or the
UpNavButton
The uses can be varied, prevent the exit of the app while there is a task in progress, compute the double press to exit the app...
The back button defaults to calling
finish()
but if you want to override this method, add code before it returns and terminates oneActivity
you must create theonBackPressed()
.There is also the way to identify when the back button is pressed
But the correct thing would be:
EDITION
When overriding the method
onBackPressed()
the user should know why he overwrites it, if he wants to return to theActivity
previous one without making any changes it does not make sense. However, I will give an example. If we want to modify the back event and warn the user "Are you sure to exit" it is advised to write this method AND tell the method that it has to return to theActivity
previous one or to the one the user wants.I quote @FabioVenturiPastor
As explained above, if the user overrides the method and doesn't do any additional code and doesn't specify that it
activity
should return, it doesn't make any sense.finish()
finish() : when this method is called the Activity is closed and destroyed. This method can be called wherever it is needed, for example triggered by interacting with some element in the UI or by performing some action in our application.
onBackPressed()
onBackPressed() : When you implement this method unlike finish() it is called exclusively when you click the "back" button and you don't need to call finish() explicitly within it, this is if you call
super.onBackPressed()
it because it internally callsfinish()
.if you don't call
super.onBackPressed()
and want to terminate the Activity, you would have to callfinish()
:Something important is that both methods, being in an Activity and having an empty back stack , can close the application if onBackPressed() calls finish() .
As a summary, finish() ends the Activity and destroys it, while onBackPressed() can detect when we activate the "back" button, it detects the "back" event and internally executes
finish()
.The finish() method will terminate the activity and the onBackPressed() method will be executed when the user presses the back softkey. To complement and since there is not much information about these methods for fragments, which is what I work with. If we want to finish the activity in a fragment it will be: "It is in a fragment"
If you work with fragments and you want an alert to appear when pressing the back button that says whether or not you want to exit the application, the code must be done in the main activity where all the fragment calls began. "it is in the main_activity":