Reputation: 728
I have a "Close" button which closes the application. I have tried 2 methods but they both are very slow.
activity.finish()
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
How can I close the activity or send it to the background faster?
Why clicking on the "home" button is much faster than sending ACTION_MAIN
intent?
Upvotes: 0
Views: 952
Reputation: 3673
Pressing the HOME Button will call onPause()
on the current Activity
. Thus it's not closing the app in total but pause it.
Ergo: It is of course much faster even in starting because it keeps the memory.
Now you know how to pause the app instead of closing if you want to have a fast approach.
If you want to close the whole app I would suggest to use finishAndRemoveTask();
Finishes all activities in this task and removes it from the recent tasks list.
Note: BACK Button will call onDestroy()
if you want to have another way. Try out what fits best for your usage.
Close app like HOME Button programmatically without a transparent View
using a Button
:
Button close = findViewById(R.id.myCloseButton);
close.setOnClickListener(view -> {
finishAffinity();
});
Upvotes: 1