简体   繁体   English

在执行finish()之后调用onNewIntent

[英]onNewIntent called after finish() executed

I have an application consisting of a sticky service and a separate SINGLE_TOP activity. 我有一个包含粘性服务和单独的SINGLE_TOP活动的应用程序。 The service receives messages from a server then launches the Activity using startActivity(intent). 该服务从服务器接收消息,然后使用startActivity(intent)启动Activity。 When all messages have been dismissed by the user I terminate the Activity using finish(). 当用户取消了所有消息后,我使用finish()终止活动。 I was under the impression and from my research so is everyone else that once finish() had executed any subsequent startActivity(intent) calls would come through the onCreate not the onNewIntent. 我的印象是,根据我的研究,其他所有人也是如此,一旦finish()执行了任何后续的startActivity(intent)调用,将通过onCreate而不是onNewIntent进行。 However, if I send new messages at the exact time I dismiss the last message I (albeit rarely) see an onNewIntent after the finish() has executed but prior to the onDestroy. 但是,如果我在正确的时间发送新消息,那么我会关闭最后一条消息(尽管很少),我会在执行finish()之后但在onDestroy之前看到onNewIntent。

Has anyone experienced this situation and is there a simple solution? 有没有人遇到过这种情况,有没有简单的解决方案?

The solution I am contemplating would be to: 1. tell the service that the Activity is stopping prior to executing the finish() 2. Wait for an OK from the service. 我正在考虑的解决方案是:1.在执行finish()之前告知服务该Activity正在停止。2.等待该服务的OK。 3. the service would then queue any messages until it was informed the Activity had stopped 4. have the activities onDestroy inform the service it has stopped 5. service would send any queue'd messages. 3.然后,该服务将对所有消息进行排队,直到通知该活动已停止为止。4.使活动onDestroy通知该服务已停止。5.服务将发送任何已排队的消息。

It would all be much simpler if finish() gaurenteed an onCreate. 如果finish()保证一个onCreate的话,一切都会简单得多。

Thanks for any input. 感谢您的任何投入。

I'll explain you about singleTop . 我将向您介绍singleTop

Consider Activities A,B and C. Let us assume that B is singleTop and other two are standard . 考虑活动A,B和C。让我们假设B是singleTop ,其他两个是standard So this is how that stack will look. 这就是该堆栈的外观。

A -> B -> C -> B -> new Instance of B will be created

A -> B -> C -> B -> B -> onNewIntent() called, new Instance of B is NOT created

finish() in turn calls onDestroy() so in both the cases your activity is removed from top of the stack. finish()依次调用onDestroy()因此在两种情况下,您的活动都将从堆栈的顶部删除。

Maybe in your case this is happening : 也许在您的情况下,这正在发生:

A -> B -> C -> finish() -> B -> onNewIntent() called

A -> B -> C -> B -> finish() -> finish() -> B -> onNewIntent() called

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

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