简体   繁体   English

在重写onNewIntent时是否有任何理由不调用setIntent?

[英]Is there any reason not to call setIntent when overriding onNewIntent?

While experiencing a problem similar to this question , I began to wonder why we explicitly have to call setIntent when overriding onNewIntent , and why this code isn't performed already by super.onNewIntent . 虽然经历了类似的问题这个问题 ,我开始怀疑,为什么我们明确要调用setIntent重写时onNewIntent ,为什么由不执行该代码已经super.onNewIntent

@Override
public void onNewIntent(Intent intent)
{
  super.onNewIntent(intent);

  // Why isn't this performed by the framework in the line above?
  setIntent(intent);
}

Intent objects are persistently attached to the Activity , Service and other components for as long as those components are running. 只要这些组件正在运行, Intent对象就会持久地附加到ActivityService和其他组件。 They don't simply go away because you moved off to another application. 他们不会因为你搬到另一个应用程序而离开。 The reason for this is because Android may kill the process at any time, but the user may still want to go back and continue what they were doing. 这是因为Android可能会随时终止该进程,但用户可能仍希望返回并继续他们正在做的事情。 This makes Intents ideal for storing or transmitting small (and sometimes large) bits of information, via the Extras. 这使得Intents非常适合通过Extras存储或传输小的(有时是大的)信息。

The onNewIntent() method is specifically for handling application components that are more persistent and hence may be called more than once during its LifeCycle but needs to keep track of the reasons why it was called (and hence the data it was called with). onNewIntent()方法专门用于处理更持久的应用程序组件,因此可以在其LifeCycle期间多次调用,但需要跟踪调用它的原因(以及调用它的数据)。 Whether you call setIntent() or not depends on what you need to do. 是否调用setIntent()取决于您需要做什么。

If you don't care why it was subsequently called, you may keep the original Intent by not calling setIntent() . 如果您不关心为什么随后调用它,您可以通过不调用setIntent()来保留原始Intent This is particularly useful when your Activity (or some other component) does the same thing no matter who called it and what data it provides. 当你的Activity (或其他一些组件)做同样的事情时,无论是谁调用它以及它提供了什么数据,这都特别有用。

If you have a need to respond to each event individually, then you must at least store the new Intent 's information. 如果您需要单独响应每个事件,那么您必须至少存储新Intent的信息。 This means you may avoid setIntent() , however, then none of the components it links to will have any of the Intent information unless you send it to them directly. 这意味着您可以避免使用setIntent() ,但是,它链接到的任何组件都不会包含任何Intent信息,除非您直接将它们发送给它们。 This may be the desired behavior for an application that cannot insure the original Intent was completely handled. 对于无法确保完全处理原始Intent的应用程序,这可能是所需的行为。

If you need to respond to each Intent individually and the original Intent does not matter, then you use setIntent() . 如果您需要单独响应每个Intent并且原始Intent无关紧要,那么您使用setIntent() This discards the original Intent , which is still in there... and places the new Intent so that if the user moves away (yet again), they will come back to the same place. 这会丢弃原来的Intent ,它仍在那里......然后放置新的Intent这样如果用户离开(再次),它们将回到同一个地方。

The reason why super.onNewIntent() does not handle this is because the core component classes cannot determine whether or not the new Intent is more important than the old one. super.onNewIntent()不处理这个问题的原因是核心组件类无法确定新的Intent是否比旧的Intent更重要。 All it cares is that it has an Intent , not what it is. 所有它关心的是它一个Intent ,而不是它。 This is why we override methods like these, so that we determine what is important and what is not. 这就是我们覆盖这些方法的原因,以便我们确定哪些是重要的,哪些不是。 The general feeling is that base classes like Activity can use whatever data we have, in whatever ways it wants to (unless we override and tell it otherwise). 一般的感觉是像Activity这样的基类可以以任何方式使用我们拥有的任何数据(除非我们覆盖并告诉它)。 However, they should not (and often cannot) get rid of our data unless we tell them to specifically. 但是,除非我们具体告诉他们,否则他们不应该(通常也不能)摆脱我们的数据。 This is an argument you really don't want to have with some programmers. 这是一个你真的不希望与某些程序员有关的论点。 hehe 呵呵

Hope this helps. 希望这可以帮助。

The docs for onNewIntent state: "Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent." onNewIntent状态的文档:“请注意,getIntent()仍返回原始Intent。您可以使用setIntent(Intent)将其更新为此新Intent。” My guess is that onNewIntent exists so that your activity can be notified and the super probably does nothing. 我的猜测是onNewIntent存在,以便您的活动可以被通知,超级可能什么都不做。

I've just removed the setIntent(intent) code from my onNewIntent(Intent intent) implementation. 我刚刚从onNewIntent(Intent intent)实现中删除了setIntent(intent)代码。

The reason: My MainActivity is single top. 原因:我的MainActivity是单顶。 While my app runs it launches other activities like the camera activity. 当我的应用程序运行时,它启动其他活动,如相机活动。 When the app returns to the MainActivity, in onResume() the (last) intent returned by getIntent() would be re-processed even if it would already have been processed before. 当应用程序返回到MainActivity时,在onResume()getIntent()返回的(最后)意图将被重新处理,即使它之前已经处理过。

My new implementation (working so far): Store the intent from onNewIntent(Intent intent) in a private instance field. 我的新实现(到目前为止工作):将onNewIntent(Intent intent)中的intent存储在私有实例字段中。 In onResume check this field for not null and reset it immediately to null, processing the intent once and only once. onResume检查此字段是否为null并立即将其重置为null,只处理​​一次intent。

See also https://groups.google.com/forum/#!topic/android-developers/vrLdM5mKeoY 另请参阅https://groups.google.com/forum/#!topic/android-developers/vrLdM5mKeoY

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

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