简体   繁体   English

发送意图后,为什么要调用finish()方法?

[英]Why should we call finish() method after sending an intent?

I just came across this code in 我刚在

Intent wizard = new Intent();
wizard.setClass(this, A.class);
wizard.putExtra("Domain", A.getInstance().B);
startActivity(wizard);
finish();

Why should we call finish() method here? 我们为什么要在这里调用finish()方法? What's it purpose? 目的是什么?

finish() Call this when your activity is done and should be closed, when starting an activity calling finish() will close the current activity and it will not be available in the Activity Stack. finish()活动并应关闭时调用此函数,启动活动时调用finish()将关闭当前活动,并且在Activity堆栈中将不可用。

在此处输入图片说明

finish(); 

finish用于关闭当前活动。就像您正在将数据发送到其他活动/打开新活动并关闭当前活动一样。

You are not imperative forced to do that... normally people do that if and only if the want to destroy the activity that is starting the intent, but that must no be your case... 您不是必须要这样做的。通常,只有当人们想要破坏启动意图的活动时,人们才这样做,但这绝对不是您的情况……

in the life cycle of android you will see: 在android的生命周期中,您将看到:

assuming you activity is visible, then calling finish() method will call 假设您的活动是可见的,则调用finish()方法将调用

onPause() , then onStop() and finally the onDestroy() onPause() ,然后是onStop() ,最后是onDestroy()

在此处输入图片说明

Let's understand it using few quotes 让我们用几个引号来理解它

finish - Call this when your activity is done and should be closed. 完成 - 完成活动并应将其关闭时调用此方法。 The ActivityResult is propagated back to whoever launched you via onActivityResult() 通过onActivityResult()将ActivityResult传播回发起您的人


onDestroy() - The final call you receive before your activity is destroyed. onDestroy() -在活动销毁之前收到的最后一个呼叫。 This can happen either because the activity is finishing ( someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. 发生这种情况的原因可能是活动即将结束( 有人在其上执行finish() ,或者因为系统暂时销毁了此活动实例以节省空间。

when you call finish() on an activity, the methods followed by onDestroy() is executed.. eg: onPause() > onStop() > and onDestroy() they can be differ from the place you call finish() ! 当您在活动上调用finish()时,将执行紧随onDestroy()的方法。例如: onPause() > onStop() >和onDestroy()它们可能与您调用finish()的地方不同!

onDestroy() is meant for final cleanup - freeing up resources that you can on your own,closing open connections,readers,writers,etc. onDestroy()用于最终清理-释放您可以自己使用的资源,关闭打开的连接,读取器,写入器等。 If you don't override it, the system does what it has to. 如果您不覆盖它,则系统将执行它必须执行的操作。

It inform the system that you want to finish the selected Activity so it wil call onDestroy() after you finish the activity.( but this does not mean that onDestroy() only gets called by finish(),system can do that when it's running out of resources after the activity is sent to the back state ) 它告诉您要完成所选择的活动所以它西港岛线调用系统onDestroy()完成该活动后。( 但是,这并不意味着onDestroy()只得到由完成()调用时,系统能做到这一点时,它的运行活动发送到后台状态后资源不足

Also there are more interesting questions like Activity.finish() called but activity stays loaded in memory that you might like 此外,还有一些更有趣的问题,例如调用了Activity.finish(),但活动仍保持加载在您可能喜欢的内存

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

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