3

Is there a way to exit android application from the Application class itself. This is even before any activity has been initialized.

Scenario is user side-loading the build in an unsupported device which leads to crashes when I try to load third-party library not meant for the device.

This loading of third-party SDK happens in the application class.

Is it safe to use System.exit(0) in this case since I cannot call finishAffinity()?

Raghav
  • 1,014
  • 2
  • 16
  • 34
  • 1
    System.exit() does not kill your app if you have more than one activity on the stack. – Laith Dec 21 '21 at 21:57
  • As I mentioned in the question, this is done in application onCreate() much before any activity is created – Raghav Dec 21 '21 at 22:19
  • System.exit() is a bad way of termination of android app, I would just not use it to be honest. – Laith Dec 21 '21 at 22:23
  • I recommend using finishAndRemoveTask(); instead of System.exit() – Laith Dec 21 '21 at 22:27
  • Do you need to do this before the activity gets initialized? Asking this because you can just finish the activity as soon as it gets created with failed initialization. Unless the activity itself (before onCreate) requires this, just detect this on app class and then finish the activity if it has failed (and check your onCreate -> onStart -> onResume -> onPause -> onStop -> onDestroy cycle for null pointers.) Calling System.exit() or any kind of equivalent will lead to unpredictable behavior. – Furkan Yurdakul Dec 27 '21 at 14:22
  • Please check [this answer](https://stackoverflow.com/a/45356090/7948109), please also check @user1506104 answer below, I tested `killProcess` it flicker in some devices – Rahul Gaur Dec 28 '21 at 05:02

3 Answers3

1

Quick answer: You can use any of the functions to exit an app mentioned in this post. You can call your exit function in the onCreate() function of your Application class. However, all of them will result to a flicker in unsupported devices. This is not a graceful way to exit an app.

Preferred answer: So, as an alternative, show an activity that explains why the app can't continue to load. If it is possible, move your library loading in another class. Use its return value to determine if the loading happened successfully. If successful, continue loading else show an error message then exit the app.

user1506104
  • 6,554
  • 4
  • 71
  • 89
0

I believe this will serve your purpose.

 android.os.Process.killProcess(android.os.Process.myPid());
Junior
  • 1,007
  • 4
  • 16
  • 26
0

Try to kill all processes first, then close java vm.

public void killAppProcess() {
    ActivityManager mActivityManager = (ActivityManager)CurrentActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo>  mList = mActivityManager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : mList)
    {
        if (runningAppProcessInfo.pid != android.os.Process.myPid())
        {
            android.os.Process.killProcess(runningAppProcessInfo.pid);
        }
    }
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(0);
}
BitByteDog
  • 3,074
  • 2
  • 26
  • 39
chenmo
  • 188
  • 1
  • 5