简体   繁体   English

为什么我们需要将Intent过渡到新的Activity / Service?

[英]Why do we need Intents for transition to new Activity/Service?

I was asked the following question in an interview : 在采访中有人问我以下问题:

Why do we need Intents to start new Activity or Service? 为什么我们需要意图来启动新的活动或服务? Alternatively, why don't we start Activity by creating a new object and calling activity_obj.onCreate(..); 另外,为什么不通过创建一个新对象并调用activity_obj.onCreate(..)来启动Activity? on it. 在上面。

I could count the benefits of intents like transferring data and intent-filters targeting appropriate activities, but except that I couldn't come up with a satisfying answer. 我可以指望诸如传输数据和针对适当活动的意图过滤器之类的意图的好处,但是除了我无法给出令人满意的答案之外。

Is it related to life cycle callback handling of Activities ? 它与活动的生命周期回调处理有关吗?

The way Android starts Activities is through Zygotes and a whole lot of Java configuration. Android启动活动的方式是通过Zygotes和大量Java配置进行的。 I feel like it would be a big hot mess if we were to do all the dirty work configuring and starting activities. 我觉得如果我们要完成所有肮脏的工作来配置和启动活动,那将是一个很大的麻烦。 This way with intents we can make "system-calls" which just hand over to Android for a bit and ask it to do some stuff. 通过这种方式,我们可以进行“系统调用”,将其移交给Android并要求它做一些事情。

From a security point of view, it also means that each Activity is started by Android. 从安全角度来看,这还意味着每个Activity由Android启动。 Which means you can't start Whatsapp, to share some text, and have full root control over it because you started it. 这意味着您无法启动Whatsapp,无法共享某些文本,也无法完全控制它,因为您已经启动了它。 This is very beneficial, and Android even provides a way for you to interact with your own Activities if needs be by communicating within the same process. 这是非常有益的,如果需要,您甚至可以通过在同一过程中进行通信,Android甚至为您提供了一种与自己的活动进行交互的方式。 This Sandboxed approach is one of the elements that helps the platform's security. 这种沙盒方法是有助于平台安全的要素之一。

The main reason is that this gives Android an easy way to fully customize the user experience. 主要原因是,这为Android提供了一种轻松的方式来完全自定义用户体验。 By the use of "implicit Intents", your application can tell Android that it should find a component that is capable of performing a certain type of ACTION on a certain type of data (for example, "VIEW an URL" or "OPEN a PDF-Document" or "COMPOSE an EMAIL"). 通过使用“隐式意图”,您的应用程序可以告诉Android它应该找到一个能够对特定类型的数据执行特定类型ACTION的组件(例如,“查看URL”或“打开PDF” -文档”或“撰写电子邮件”)。 Your app doesn't need to know what component to launch to do this. 您的应用无需知道要启动哪个组件来执行此操作。 In fact, the user may have several components installed on his device that can perform the desired ACTION, so this allows Android to ask the user what component should be used to perform the desired ACTION. 实际上,用户可能在其设备上安装了可以执行所需ACTION的几个组件,因此这使Android可以询问用户应使用哪个组件来执行所需ACTION。 This makes it incredibly easy to provide drop-in replacements for almost all the apps on your device. 这使得为​​设备上几乎所有应用程序提供即插即用替代品变得异常容易。

Of course there are 25 other ways to accomplish this. 当然,还有25种其他方式可以完成此操作。 The original developers chose the Intent mechanism. 最初的开发人员选择了Intent机制。

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

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