Close application and launch home screen on Android

Solution 1:

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.

Solution 2:

The easiest way for achieving this is given below (without affecting Android's native memory management. There is no process killing involved).

  1. Launch an activity using this Intent:

    Intent intent = new Intent(this, FinActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();
    
  2. In the target activity FinActivity.class, call finish() in onCreate.

Steps Explained:

  1. You create an intent that erases all other activities (FLAG_ACTIVITY_CLEAR_TOP) and delete the current activity.

  2. The activity destroys itself. An alternative is that you can make an splash screen in finActivity. This is optional.

Solution 3:

You should really think about not exiting the application. This is not how Android apps usually work.