简体   繁体   English

可以在finish()之后恢复Android活动吗?

[英]Can an Android Activity be resumed after finish()?

Checking some legacy code I've found this snippet: 检查一些遗留代码我发现这个片段:

@Override
public void onResume() {
    if (!isFinishing()) {
        ...
    }
    super.onResume();
}

despite the super.onResume() call at the end of the method, which is discouraged: 尽管在方法结束时调用了super.onResume() ,但不鼓励:

Note: Your implementation of these lifecycle methods must always call the superclass implementation before doing any work, as shown in the examples above http://developer.android.com/guide/components/activities.html 注意:在执行任何工作之前,您对这些生命周期方法的实现必须始终调用超类实现,如上面的示例所示http://developer.android.com/guide/components/activities.html

I'm concerned about the if (!isFinishing()) call, does this have sense? 我关注if (!isFinishing())调用,这有意义吗? as I can see checking Activity code mFinished variable is set to true only on finish() and finishActivity() , can, through the Android Lifecycle, to be resumed an activity which is being destroyed? 我可以看到检查活动代码mFinished变量仅在finish()finishActivity()上设置为true,可以通过Android生命周期恢复正在销毁的活动吗?

Thanks in advance. 提前致谢。

The answer to you question is "no" activity cannot be resumed if it was destroyed. 你回答的问题是“不”活动如果被销毁就无法恢复。
Here is good discussion: Understanding of isFinishing() 这是一个很好的讨论: 了解isFinishing()

The reason for this code may be to distinguish between orientation change and actual finishing of the activity Important to note here is isFinishing: true which means that call to isFinishing() in the onDestroy() returns true, ie which happens when: 这段代码的原因可能是区分方向更改和活动的实际完成。重要的是要注意这里isFinishing:true表示在onDestroy()中调用isFinishing() ()会返回true,即在以下情况下发生:

User hits "back" button OR activity's code calls it's finish() ( isFinishing() returns false when activity is geting closed after phone rotaion in order to be started again) 用户点击“返回”按钮或活动的代码调用它的finish()isFinishing()在电话轮换后活动关闭时返回false,以便再次启动)

http://ogrelab.ikratko.com/activity-lifecycle-explained-in-details/ http://ogrelab.ikratko.com/activity-lifecycle-explained-in-details/

Finally, the legacy code was calling finish() under some circunstances at the onCreate() method. 最后,遗留代码在onCreate()方法的某些环境下调用finish() But taking a look at onCreate() javadoc : 但是看看onCreate() javadoc

You can call finish() from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing. 您可以在此函数中调用finish(),在这种情况下,将立即调用onDestroy(),而不执行任何其余的活动生命周期(onStart(),onResume(),onPause()等)。

So, this isFinishing() call is useless inside onResume() 所以,这个isFinishing()调用在onResume()里面没用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM