Reputation: 1064
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()?
Upvotes: 4
Views: 1041
Reputation: 188
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);
}
Upvotes: 0
Reputation: 7076
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.
Upvotes: 1
Reputation: 1293
I believe this will serve your purpose.
android.os.Process.killProcess(android.os.Process.myPid());
Upvotes: 0