I want to go to the home screen programmatically in Android when the user clicks on button. How can this be done?
addFlags(Intent. FLAG_ACTIVITY_CLEAR_TASK); Now when you pressed the back button being MainActivity the active one, it will go to home screen.
Go to Home screenSwipe up from the bottom of the screen. Tap Home or Home .
use 'System. exit(0);' when you really want to exit the app.
You can do this through an Intent.
Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain);
This Intent will start the launcher application that the user has defined. Be careful with this because this will look like your application crashed if the user does not expect this.
If you want this to build an exit button from your app please read this article on exit Buttons in Android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With