Why is finish() not quitting processing immediately?

99封情书 提交于 2020-01-04 04:19:04

问题


Is there any particular situation when it is handy to not quit the Activity immediately after calling finish() ?

(in other way, why is this method not quitting the Activity immediately from design?)

http://developer.android.com/reference/android/app/Activity.html#finish()

UPDATE

When I say immediately, I mean, right in time you will call the finish() and of course, cleaning up with saving instance bundle, onStop and onDestroy methods doesn't count.

To see an example what am I talking about, here is the snippet

onCreate(Bundle savedInstance){
    // code executed
    if(somecondition){
        finish();
    }
    // code which shouldn't be executed
}

the question is, why is the code after condition finished as well before ending activity, and why the finish() call not stopping the processing immediately


回答1:


Shouldn't you do a return to prevent the codes below to execute:

if(somecondition){
    finish();
    return;
}



回答2:


Imagine you have a thread_0 with a loop handling all the events.

Calling finish adds a new event, but the activity isn't killed until thread_0 handles the event.




回答3:


Something to check: are you running any Async tasks that are not finished?




回答4:


finish() just hides the activity but not killed immediately. And makes it available for killing at later when os will need some memory to run other apps or processes.




回答5:


I guess Android allows you to save some data even after finish(). I think cancelling could also hurt the thread. In your case you could use return; to stop the code from executing.



来源:https://stackoverflow.com/questions/8679278/why-is-finish-not-quitting-processing-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!