Android 4.4.2 - java.lang.RuntimeException:正在停止未恢复的活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21947675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:21:15  来源:igfitidea点击:

Android 4.4.2 - java.lang.RuntimeException: Performing stop of activity that is not resumed

androidandroid-activity

提问by user3072558

I'm getting this exception on a 4.4.2 device. Not reproducible on Android 4.3 device or lower.

我在 4.4.2 设备上收到此异常。无法在 Android 4.3 或更低版本的设备上重现。

Setup is I have a home activity (subclass of support ActionBarActivity). The home activity checks a boolean flag, and if true, launches a splash screen activity (yes, ideally the splash comes before the home activity, but let's assume I can't change it to work that way for now).

设置是我有一个家庭活动(支持的子类ActionBarActivity)。home 活动检查一个布尔标志,如果为真,则启动一个闪屏活动(是的,理想情况下,闪屏出现在主页活动之前,但假设我现在无法将其更改为那样工作)。

The splash screen is launched with startActivityForResult, it downloads some config options from the server, then finishes and returns the result back to the home activity.

启动画面用 启动startActivityForResult,它从服务器下载一些配置选项,然后完成并将结果返回到主页活动。

Weird thing is this works fine on 4.3 and below, but on 4.4 devices, I get the above exception (full stack trace):

奇怪的是,这在 4.3 及以下版本上运行良好,但在 4.4 设备上,我收到上述异常(完整堆栈跟踪):

02-21 13:36:16.733  24409-24409/test.player E/ActivityThread﹕ Performing stop of activity that is not resumed: {test.player/test.ui.actvities.HomeActivity}
    java.lang.RuntimeException: Performing stop of activity that is not resumed: {test.player/test.ui.actvities.HomeActivity}
            at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3147)
            at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3234)
            at android.app.ActivityThread.access00(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

Based on the above, it looks like onStop (because I launch the splash activity on onCreate) is called before onResume for the Home Activity.

基于上述,看起来 onStop(因为我在 onCreate 上启动了启动活动)在 Home Activity 的 onResume 之前被调用。

Why is this now causing problems in 4.4.x?

为什么这会导致 4.4.x 出现问题?

采纳答案by user3072558

That doesn't seem right to me. The splash activity would now be the top activity in the stack, so the HomeActivityonStop lifecycle method would get called eventually. Coincidentally, I moved the startActivitycall for the splash activity from onCreateto onResumein the HomeActivity, and the error goes away.

这对我来说似乎不对。飞溅活动现在将是堆栈中的顶部活动,因此HomeActivity最终将调用 onStop 生命周期方法。巧合的是,我将startActivity飞溅活动的调用从onCreate移到onResumeHomeActivity,错误消失了。

回答by sheetal

This issue will still be present on all high-end phones with Android 4.4.2 and above including NEXUS 5 and Samsumg s4 since onResume gets called but it is still in animation stage.So if you try to start a activity in onResume the issue will replicate.

由于 onResume 被调用但它仍处于动画阶段,因此该问题仍将出现在所有搭载 Android 4.4.2 及更高版本的高端手机上,包括 NEXUS 5 和 Samsumg s4。因此,如果您尝试在 onResume 中启动活动,问题将复制。

Put your switching activity in a handler delayed method.

将您的切换活动放在处理程序延迟方法中。

    Handler handler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
              //Start another Activity Here

            default:
                break;
        }
        return false;
    }
});

And in onResume call this.

在 onResume 调用这个。

 handler.sendEmptyMessageDelayed(1, 1000);

By that time you can show loader or something or block user Interaction

到那时你可以显示加载程序或其他内容或阻止用户交互

回答by Deinlandel

I was getting this exception even when using onResume(), so I ended up overriding onPostResume()and starting activity from there, and the exception is gone. Not sure if this is an ideal solution, but still...

即使在使用时我也遇到了这个异常onResume(),所以我最终覆盖onPostResume()并从那里开始活动,异常消失了。不确定这是否是理想的解决方案,但仍然......

回答by Chris623

Just call the onResume super method before launching the new activity:

只需在启动新活动之前调用 onResume 超级方法:

super.onResume();