是否可以中断 finish()

发布于 2024-12-04 16:10:49 字数 346 浏览 1 评论 0原文

我有一个以 SingleTask 模式(Android 2.2)运行的活动,它接收来自其他活动或服务的意图。我的活动将每个收到的意图推送到队列中并对其执行某些操作。完成后,如果没有任何要处理的意图(即队列为空),它将调用 finish()。

问题是当它在调用 finish() 后立即收到意图时:调用 onNewIntent 和 onResume 方法,但我的 Activity 随后被 Android 销毁(onPause/onDestroy),并且该意图未得到处理。

我知道这是一个相当顽皮的场景,但我对这种行为感兴趣......所以问题是:是否可以在调用 finish() 后中断 finish() 和/或恢复活动?

谢谢!

I have an activity running in SingleTask mode (Android 2.2) which receives intents from other activities or services. My activity pushes each received intent into a queue and does something with it. On completion, if there isn't any intent left to process (i.e. the queue is empty) it calls finish().

The problem is when it receives an intent immediately after finish() has been called: the onNewIntent and onResume methods are called, but my activity is then destroyed (onPause/onDestroy) by Android and the intent is not processed.

I know that this is quite a naughty scenario, but I'm interested in this kind of behaviour... so the question is: is it possible to interrupt finish() and/or resume the activity after finish() has been called?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里人 2024-12-11 16:10:49

听起来您正在尝试重新创建 IntentService

It sounds like you're trying to re-create an IntentService

苏别ゝ 2024-12-11 16:10:49

在调用 finish() 时,将调用 onDestroy() 方法,然后您的活动就死了:)

该活动可以通过某种意图再次启动,但将调用 oncreate 而不是 onNewIntent

重要注意事项是,在之间无法接收意图finish() 语句和 onDrestroy 方法!

因此,如果调用完成,那么您的活动的每个新意图都会调用 onCreate(),

因此您不必担心,

您不应该尝试中断完成,这是不可能的,因为它是原子操作
但是您需要检查您的逻辑并增强您的 oncreate 和 onNewIntent 方法以优雅地处理您的场景

on call to finish() the onDestroy() method is called and than you activity is dead :)

this activity can be started again by some intent, but the oncreate will be called not onNewIntent

Important note is that the intent can not be received between the finish() statement and onDrestroy method !

so if the finish is called then every new intent for your activity will result with call to onCreate()

so you do not to be worried

you should not try to interrupt finish that is not possible cause it is atomic operation
But you need to review you logic and enhance your oncreate and onNewIntent method to gracefully handle your scenario

分开我的手 2024-12-11 16:10:49

从 onNewIntent 开始活动。

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

    if (isFinishing()) {
        // Start the activity again using the new intent
        startActivity(intent);
        return;
    }
}

Start activity from onNewIntent.

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

    if (isFinishing()) {
        // Start the activity again using the new intent
        startActivity(intent);
        return;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文