在片段中使用 OnNewIntent 方法
OnNewIntent method using in fragment
我正在使用一个使用 NFC 编写的应用程序,我一直在观看许多教程,在所有这些教程中,它们都在 activity 中使用了 onNewIntent 方法。我正在使用片段,我有两个问题:
如何在片段中使用此方法或我应该怎么做?另一个问题是,这种方法在 NFC 的情况下有什么作用?
This is called for activities that set launchMode to "singleTop" in
their package, or if a client used the Intent#FLAG_ACTIVITY_SINGLE_TOP
flag when calling startActivity(Intent). In either case, when the
activity is re-launched while at the top of the activity stack instead
of a new instance of the activity being started, onNewIntent() will be
called on the existing instance with the Intent that was used to
re-launch it.
检查Android docs。
这意味着如果你有一个像上面提到的那样的 Activity,并且你已经创建了 Activity 并出现在你的应用程序堆栈中,那么当 Activity 需要重新启动(例如,通过深层链接 URL),则不会创建新的 Activity。相反,堆栈中的 Activity 实例将移至顶部,并且 Activity 中的 onNewIntent
将使用用于创建该 Activity 的新 Intent 进行调用。
因此,如果有人连续点击多个深层链接 URL,您将不会在彼此之上出现一堆相同 Activity 的实例。相反,您将只有一个具有最新 Intent 的 Activity 实例。
我没有 NFC 方面的经验。我假设,类似于深度链接,您将从应用程序外部(可能来自传感器)接收到应用程序的一些数据,并且您使用一个 Activity 来接收和处理该数据。在这种情况下,您需要使用 onNewIntent
.
由于只能从外部启动 Activity,因此此 onNewIntent
仅适用于 Activity。如果您正在使用 Fragment,您应该在收到 onNewIntent
中的最新数据后将数据从 Activity 传递到您的 Fragment。
我正在使用一个使用 NFC 编写的应用程序,我一直在观看许多教程,在所有这些教程中,它们都在 activity 中使用了 onNewIntent 方法。我正在使用片段,我有两个问题:
如何在片段中使用此方法或我应该怎么做?另一个问题是,这种方法在 NFC 的情况下有什么作用?
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent#FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
检查Android docs。
这意味着如果你有一个像上面提到的那样的 Activity,并且你已经创建了 Activity 并出现在你的应用程序堆栈中,那么当 Activity 需要重新启动(例如,通过深层链接 URL),则不会创建新的 Activity。相反,堆栈中的 Activity 实例将移至顶部,并且 Activity 中的 onNewIntent
将使用用于创建该 Activity 的新 Intent 进行调用。
因此,如果有人连续点击多个深层链接 URL,您将不会在彼此之上出现一堆相同 Activity 的实例。相反,您将只有一个具有最新 Intent 的 Activity 实例。
我没有 NFC 方面的经验。我假设,类似于深度链接,您将从应用程序外部(可能来自传感器)接收到应用程序的一些数据,并且您使用一个 Activity 来接收和处理该数据。在这种情况下,您需要使用 onNewIntent
.
由于只能从外部启动 Activity,因此此 onNewIntent
仅适用于 Activity。如果您正在使用 Fragment,您应该在收到 onNewIntent
中的最新数据后将数据从 Activity 传递到您的 Fragment。