I had the same problem with my application when the onCreate () method of the Application class only started the first time my application loaded. Daniel's decision to use System.exit (0) did the trick, but this solution led me to another problem. After using System.exit (0), the onPause (), onStop (), and onDestroy () method of my foreground activity was not called.
Well, that was reasonable behavior for the application, because if you use System.exit (0), the application will be removed from the System process queue, and there will be no way for the android to execute onPause (), onStop () and onDestroy () for mine foreground work.
The workaround I used for this problem was to end my activity when the back button was pressed and after a while kill my application process, as shown below:
public void killApp(){ final Thread threadKillApp = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Log.i(TAG, "Going to kill app"); android.os.Process.killProcess(android.os.Process.myPid()); } }); threadKillApp.start(); }
The call to the killApp () method right after the finish () function was called in my work completed the task.
source share