1

I want to go to the home screen(Minimize App) programmatically in Android when the user clicks on button How can this be done?

I have Tried this approach but its giving Fatal Exception: java.lang.SecurityException Exception for some devices.

Can anyone help me out with this?

  • How is this useful for a user? If user wants to go home, he can just press the home button. – m0skit0 Mar 27 '20 at 10:18
  • Does the app have to be paused? Otherwise, just call 'finish()' and the app is closed. – bytesculptor Mar 27 '20 at 10:19
  • when we press home button it will pause app and minimize app,i want this behaviour – Rajendra Dabhi Mar 27 '20 at 10:20
  • Just call finish() then – m0skit0 Mar 27 '20 at 10:23
  • @m0skit0 its a requirement of client,he dont want that all activity got closed he just want to minimize, – Rajendra Dabhi Mar 27 '20 at 10:23
  • finish() will kil activity not minimize – Rajendra Dabhi Mar 27 '20 at 10:24
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Mar 27 '20 at 10:35

2 Answers2

0

When you press back button, you can use this to go to home screen of phone

private fun goToHomeScreen() {
    val startMain = Intent(Intent.ACTION_MAIN)
    startMain.addCategory(Intent.CATEGORY_HOME)
    startMain.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    startActivity(startMain)
}

But its not a best practice of programming because if phone doesn't have more memory then your app will be automatically killed in the background. Your app will be resumed if there's memrory left in the phone.

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
0

You can use the following method

this.moveTaskToBack(true); //if you are inside activity

Elsewhere

activity.moveTaskToBack(true); //activity is the reference of your Activity.

Read more about this method here moveToBack)

Jayanth
  • 5,954
  • 3
  • 21
  • 38