有什么理由不覆盖onNewIntent的时候调用setIntent?有什么、理由、时候、onNewIntent

由网友(你是命不是梦)分享简介:尽管经历类似这个问题的一个问题,我开始怀疑,为什么我们明确要调用 setIntent 覆盖时, onNewIntent ,以及为什么code ISN'牛逼已经完成由 super.onNewIntent 。@覆盖公共无效onNewIntent(意向意图){super.onNewIntent(意向);//为什么不执行这...

尽管经历类似这个问题的一个问题,我开始怀疑,为什么我们明确要调用 setIntent 覆盖时, onNewIntent ,以及为什么code ISN'牛逼已经完成由 super.onNewIntent

  @覆盖
公共无效onNewIntent(意向意图)
{
  super.onNewIntent(意向);

  //为什么不执行这个由线之上的框架?
  setIntent(意向);
}
 

解决方案

意图对象持久连接到活动服务和其他组件,只要这些组件运行。他们不只是离开,因为你到另一个应用程序移出。这样做的原因是由于Android可能会杀掉该进程在任何时间,但用户可能还是想回去继续他们在做什么。这使得意图非常适合通过附加存储或传输的信息的小(有时大)位。

onNewIntent()方法是专门用于处理那些更持久的,因此它的生命周期中可被称为一次以上应用程序组件,但需要跟踪的原因为什么它被称为(并且因此数据,它被称为带)。不管你叫 setIntent()与否取决于你需要做的。

如果你不在乎它为什么后来叫,你可以保持原有的意图不是通过调用 setIntent()。这是非常有用的,当你的活动(或其他组件)做同样的事情,不管谁把它称为,它提供了什么样的数据。

原因 无法通过方法调用转换将实际参数int转换为String

如果您有需要单独应对每个事件,则必须至少保存新意图的信息。这意味着你可能避免 setIntent(),但是,再没有它链接到会有任何的组件意图除非你直接发送给他们的信息。这可能是无法保证原来的意图完全处理的应用程序所需的行为。

如果你需要单独应对每一个意图和原意图没关系,那你就用 setIntent()。这会放弃原来的意图,它仍然在那里......并将新的意图因此,如果用户移开(再一次),他们将回到同一个地方。

之所以 super.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.

@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. 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. This makes Intents ideal for storing or transmitting small (and sometimes large) bits of information, via the 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). Whether you call setIntent() or not depends on what you need to do.

If you don't care why it was subsequently called, you may keep the original Intent by not calling setIntent(). 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.

If you have a need to respond to each event individually, then you must at least store the new Intent's information. 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. This may be the desired behavior for an application that cannot insure the original Intent was completely handled.

If you need to respond to each Intent individually and the original Intent does not matter, then you use 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.

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. All it cares is that it has an Intent, not what it is. 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). 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.

阅读全文

相关推荐

最新文章