Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit Android application from application class (Not activity)

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()?

like image 475
Raghav Avatar asked Dec 17 '21 18:12

Raghav


1 Answers

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.

like image 194
user1506104 Avatar answered Oct 05 '22 11:10

user1506104