android - 为什么我们需要一个 Intent 来创建一个 Activity ?

标签 android android-intent android-activity

我是 android 的新手,我无法在 android 中找到 Intent 的用途。

我能看懂代码:

   Intent i = new Intent(getApplicationContext(),Myclass.this);
   startActivity(i);

它的作用。但我的问题是为什么我们需要 Intent 来创建一个 Activity。为什么 Android 用户不能允许我们直接创建 Activity 而不是使用 Intent

最佳答案

这更像是一个 Android 设计问题。背后的想法是,您总是只填充一个intent(ion)“我想查看此 URL”“我想开始我的主屏幕启动器”。系统会检查哪些应用能够满足此请求,如果有多种可能性,系统会允许您选择其中之一。

从某种角度来看,这更灵活一些。例如。在我上面的第一个例子中,你不需要知道 android 标准浏览器是否存在或者是否安装了 Chrome。您只需要求系统查看一个 URL:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);

虽然有一些好处,但另一方面它可能看起来有点复杂。

关于android - 为什么我们需要一个 Intent 来创建一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15460824/

相关文章:

android - 如何通过从另一个应用程序共享音频来共享音频

android - 如何在构建服务器和工作站之间同步Gradle/Android版本代码

java - 在 webview 中,我想在应用程序本身中重定向电子邮件和 URL

android - 用相机拍照后 Intent 为空

Android ActivityGroup 菜单问题

android - 将多个数据传递给 Activity ?

Android - 总是从通知返回到当前 Activity ?

java - ArrayList IndexOutOfBoundsException 在位置 0

android - android中的虹膜识别

android - getIntent.getExtras.getString() 中的空值