Issue
I have a "Close" button which closes the application. I have tried 2 methods but they both are very slow.
- Finishing the activity:
activity.finish()
- Sending Home Page intent (from here)
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?
Solution
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.
CODE EXAMPLE
Close app like HOME Button programmatically without a transparent View
using a Button
:
Button close = findViewById(R.id.myCloseButton);
close.setOnClickListener(view -> {
finishAffinity();
});
Answered By - Etienne Kaiser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.