开发者

Understanding of isFinishing()

I got confused after reading the Android doc about isFinishing() at http://developer.android.com/reference/android/app/Activity.html.

If I call isFinishing() in onPause(), what would the return value of the isFinishing() call be in the following 3 scenarios?

  1. Activity is killed due to fi开发者_Python百科nish() being called.
  2. An activity is not in the foreground and the activity (not the app) is being killed by the OS due to low memory.
  3. Activity is going to the background.

I am sure that the result of isFinishing() will be true in scenario 1 and will be false in scenario 3.

How about scenario 2? Will isFinishing() return true or false in scenario 2?


Your Activity doesn't get killed by the OS while it's in the foreground. That wouldn't make sense.

However, if the activity goes to the background because the user switched to a different app, it could get killed after onPause() has been processed. As such, you could get isFinishing() == false as the user switches to a new app, but then the app is killed.

As the doc says, save all persistent data in onPause(). onDestroy() is not guaranteed to be called.

(I wouldn't be surprised if Dianne steps in and corrects me here, btw.)


How to call onPause? 2 ways: The activity goes to background or we call finish() somewhere. If the activity goes to background, isFinishing() is false. If we call finish(), isFinishing() is true. And now see scenario 2: An activity is not in the foreground and the activity (not the app) is being killed by OS due to low memory. An activity will be killed, so it muse be in background, it is paused, so onPause will not called. BTW, an activity cannot be killed, only process can be killed


isFinishing() returns false in all methods of an activity except onDestroy().
For more information, put logs in all the activity methods like below

Log.i("onCreate",""+isFinishing);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜