I want to exit my app and I have code similar to :
public void onClickPause (View view) {
Intent salida = new Intent(Intent.ACTION_MAIN);
finish();
}
But this returns to the first activity.
I want to exit my app and I have code similar to :
public void onClickPause (View view) {
Intent salida = new Intent(Intent.ACTION_MAIN);
finish();
}
But this returns to the first activity.
Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's onCreate or onstart
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
On your first activity, call finish() right after startActivity(). That should do the trick.
EDIT : This way the first activity will be finished and the second activity will exit to the home screen/launcher.
When you move through activities in order for the back button to work as you would expect it, there's a thing called backstack that saves the order of activities. When you call finish() the top activity on backstack finishes and activity that was running before gets back. Thats why you dont exit the app, but you return to first activity. The Intent salida does nothing and you can delete it. What you have to do is to clear the backstack and then finish. Take a look at this question
If you are supporting api>16 you can just call
finishAffinity()