When Android kills an app, can a function be stopped halfway?

后端 未结 7 2062
青春惊慌失措
青春惊慌失措 2020-12-10 03:30

I am wondering: when an app is to be killed, does Android wait for the currently running function to return, or does Android stop it before it ends by itself?

相关标签:
7条回答
  • 2020-12-10 04:28

    Updated: After testing, Android will kill any thread/AsynTask that is currently running(without calling onPostExecute()/onCancelled());

    If you close Application normally. Your ui-thread stops, but If you implment your method in a Thread / AsyncTask / Service, it will continue to run until it finishes.
    However, your Thread / AsyncTask may continue running but may or may not to be fully functional if you have a call back of your instance of the application or do something to the ui of the application. This MAY give you an Exception depending on what is being done.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    int i = 0;
                    while (true) {
                        i++;
                        Thread.sleep(2000);
                        Log.i("not dead", "not dead" + i);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    

    Ran this code, and go play some games. Results were the thread will killed by the System.

    0 讨论(0)
提交回复
热议问题