Java – Why do Android applications automatically restart after runtimeException?

Why do Android applications automatically restart after runtimeException?… here is a solution to the problem.

Why do Android applications automatically restart after runtimeException?

My app runs on two 5.0 android systems.

In my development, I have an uncaught RuntimeException

When I encounter an exception, my app automatically restarts

I don’t know why my app restarts automatically? Even though I call System.exit(1).

This is the log with RuntimException

E/AndroidRuntime(23905): FATAL EXCEPTION: main
E/AndroidRuntime(23905): Process: com.oosmart.mainapp, PID: 23905
E/AndroidRuntime(23905): android.database.sqlite.SQLiteException: no such table: devices (code 1): , while compiling: drop table devices
E/AndroidRuntime(23905):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
E/AndroidRuntime(23905):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
E/AndroidRuntime(23905):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
E/AndroidRuntime(23905):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
E/AndroidRuntime(23905):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
E/AndroidRuntime(23905):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
E/AndroidRuntime(23905):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
E/AndroidRuntime(23905):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1655)
E/AndroidRuntime(23905):    at com.oosmart.mainaplication.db.DBOperation.execute(DBOperation.java:81)
E/AndroidRuntime(23905):    at com.oosmart.mainaplication.db.DevicesDB.dropTable(DevicesDB.java:154)
E/AndroidRuntime(23905):    at com.oosmart.mainaplication.db.DBHelper.DropTable(DBHelper.java:30)
E/AndroidRuntime(23905):    at com.oosmart.mainaplication.fragment.UserCenterFragment.onExitClick(UserCenterFragment.java:128)
E/AndroidRuntime(23905):    at com.oosmart.mainaplication.fragment.UserCenterFragment$$ViewBinder$6.doClick(UserCenterFragment$$ViewBinder.java:74)
E/AndroidRuntime(23905):    at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
E/AndroidRuntime(23905):    at android.view.View.performClick(View.java:4806)
E/AndroidRuntime(23905):    at android.view.View$PerformClick.run(View.java:19952)
E/AndroidRuntime(23905):    at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(23905):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(23905):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(23905):    at android.app.ActivityThread.main(ActivityThread.java:5313)
E/AndroidRuntime(23905):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(23905):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(23905):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1116)
E/AndroidRuntime(23905):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:809)

This is the log for System.exit

I/AndroidRuntime(20479): VM exiting with result code 1, cleanup skipped.
I/AndroidRuntime(23172): VM exiting with result code 1, cleanup skipped.
I/Process (24461): java.lang.RuntimeException

I use runtime grep’s logs

Update:
Even though I call android.os.Process.killProcess(android.os.Process.myPid());

The application still restarts with the new PID.
I don’t know why I can’t exit completely through the code.

Solution

It could be because you didn’t finish the previous activity, when a new activity creates an exception, it closes the current activity and opens the previous one, the runtime exception does crash your application, and you’re trying to drop a table that doesn’t even exist and even system.exit() doesn’t quit your app. It’s a best practice to let users try catch when using any complex encoding.
If I’m wrong, someone corrects me

Related Problems and Solutions