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

后端 未结 7 2115
青春惊慌失措
青春惊慌失措 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:09

    Your app may be killed by the system at any time without another line of its code being executed. According to the documentation however, there are a few methods which are not "killable":

    • onCreate()
    • onRestart()
    • onStart()
    • onResume()
    • onPause()

    As onDestroy() won't get called for sure, it is more save to use the onPause() Method to write any persistent data to storage:

    protected void onPause(){
       super.onPause();
       if(isFinishing()){
          // store data
       }
    }
    

提交回复
热议问题