3

I have long been looking for a solution on how to close an android application.

Gathering all the posts dealing with the subject, I ended up building a healthy and effective solution that I wanted to share in this post

Please, correct me if I have failed somewhere.

public static void closeApp(Activity activity) {
    //Go to home to change main android view and focus
    //You can remove this part
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);

    //finish in background many activities as possible
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        activity.finishAndRemoveTask();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        activity.finishAffinity();
    } else {
        activity.finish();
    }

    //Kill all existing app process
    activity.moveTaskToBack(true);
    android.os.Process.killProcess(android.os.Process.myPid());

    //close the app
    System.exit(0);
}

using example

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            closeApp(MainActivity.this);
        }
    });
}
le Mandarin
  • 192
  • 10
  • You want to do this for your own application or for other applications? – Jawad Zeb Jul 03 '18 at 09:14
  • This solution works for my own application – le Mandarin Jul 03 '18 at 09:17
  • So what you want to Do? you want to close other applications? or you want to close you all activities without closing services. – Jawad Zeb Jul 03 '18 at 09:23
  • It is a solution that took me 5 months to understand and develop it so I share it to be sure that no one will fall on this error. I would like to know if I am wrong in the code. – le Mandarin Jul 03 '18 at 09:27
  • For my own application I think this could be a solution as well. Intent i=new Intent(this,FinishActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); And inside finish Activity onCreate(){ finish(); } – Jawad Zeb Jul 03 '18 at 09:28
  • This solution closes the application and releases the resources. Without ever closing the services, which is rather good for me. Is there a hidden and unforeseen action? – le Mandarin Jul 03 '18 at 09:34
  • I realized that when app is in background, System.exit is not works. – Mahdi Aug 18 '19 at 08:34

1 Answers1

0

The approach you are using looks quite comprehensive when it comes to releasing the resources and completely closing the app. Although I feel its a bit over-done but if that's what your use case demands, then no issues. You can check this SO for more insights.

I have comment on following line

stopping the background running from application but, not close services

android.os.Process.killProcess(android.os.Process.myPid()); This method will kill the process with given PID. If your Services are running in this PID then they will be killed too.

Starting from Android O, several restrictions on background execution have been imposed, due to which you cannot keep services running in background. When your application exits, OS will terminate the Services as if you have called stopSelf(). As a consequence, you won't be able to keep your Services alive.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • I use this post to prevent services stopping: https://stackoverflow.com/questions/35747624/android-service-not-restarting-in-lollipop/35915926#35915926 – le Mandarin Jul 03 '18 at 15:31
  • Its device specific. Maximum duration I have observed is around 10 mins on Samsung. On google pixel its less than 1 minute. Its not easy to control closing of background process – Sagar Jul 06 '18 at 14:38