Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close application and launch home screen on Android

I have two different activities. The first launches the second one. In the second activity, I call System.exit(0) in order to force the application to close, but the first activity is automatically displayed instead of the application returning to the home screen. How can I avoid this, and get the application to return to the home screen?

like image 703
Arutha Avatar asked Jan 11 '10 13:01

Arutha


People also ask

How do I close android home screen?

Close all apps on Android Go: Swipe up from the bottom, hold, and let go. At the bottom, tap Clear all. Find your Home screen: Tap Home or Home .

Is it better to close apps or leave them open Android?

A broad consensus regarding the constant closing of Android apps is that you should avoid doing so as much as possible. It would be best if you only closed apps in the situations mentioned above. Moreover, closing background apps will negatively affect your phone's battery life and overall performance.

How do I close apps on Android without home button?

If you're using Android 10 or above, swipe up from the bottom of the home screen, hold your finger there, and then let go.


1 Answers

Short answer: call moveTaskToBack(true) on your Activity instead of System.exit(). This will hide your application until the user wants to use it again.

The longer answer starts with another question: why do you want to kill your application?

The Android OS handles memory management and processes and so on so my advice is just let Android worry about this for you. If the user wants to leave your application they can press the Home button and your application will effectively disappear. If the phone needs more memory later the OS will terminate your application then.

As long as you're responding to lifecycle events appropriately, neither you nor the user needs to care if your application is still running or not.

So if you want to hide your application call moveTaskToBack() and let Android decide when to kill it.

like image 137
Dave Webb Avatar answered Oct 02 '22 15:10

Dave Webb