Activity did not call finish? (API 23)

泪湿孤枕 提交于 2019-11-27 01:09:28

问题


I am getting the following error and i have no clue as to why its happening.

Error:

08-23 17:07:46.533  22454-22454/com.a.b.c E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.a.b.c, PID: 22454
    java.lang.RuntimeException: Unable to resume activity {com.a.b.c/com.a.b.c.MainActivity}: java.lang.IllegalStateException: Activity {com.a.b.c/com.a.b.c.MainActivity} did not call finish() prior to onResume() completing
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalStateException: Activity {com.a.b.c/com.a.b.c.MainActivity} did not call finish() prior to onResume() completing
            at android.app.Activity.performResume(Activity.java:6324)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Code:

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    System.out.println("Started");
}

I am trying to run the code on an AVD running android 6.0 (API 23), works on API 22.


回答1:


This is a bug in Android M developer preview. More details




回答2:


I'm having the same problem, the same crash with the

did not call finish() prior to onResume() completing 

error message. So I created the v23\styles.xml

<style name="AppTheme" parent="android:Theme.Translucent">
...
</style>

while the normal styles.xml has

<style name="AppTheme" parent="android:Theme.NoDisplay">
...
</style>

It works fine, no longer crashes. However, I don't know how good is this solution, to use Theme.Translucent in API 23, especially as it is defined as

Theme for translucent activities (on API level 10 and lower).

I really hope they fix this bug.




回答3:


I found a workaround. Call setVisible(true) in onStart():

@Override
protected void onStart() {
    super.onStart();
    setVisible(true);
}



回答4:


My invisible Activity shows a confirmation dialog. This did lose the material design look when I used android:Theme.Translucent.NoTitleBar.

So, based on answers above and CommonWare's blog and the Android themes definitions I use this style, which extends a normal AppCompat.Light theme:

<style name="AppTheme.NoDisplay" parent="Theme.AppCompat.Light">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
    <item name="android:windowDisablePreview">true</item>
    <item name="android:windowNoTitle">true</item>
</style>



回答5:


where android 23> https://www.youtube.com/watch?v=NAcUGwCkrcs

Manifest:

android:theme="@android:style/Theme.Translucent.NoTitleBar"
Activity extends from Activity. Not AppCompatActivity.

and for version >= 23

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){     
    getWindow().setStatusBarColor(getResources().getColor(android.R.color.transparent))}



回答6:


It is due to setting a theme on an Activity that the user cannot see. I guess the reason Android does this is so you can't just run an invisible Activity indefinitely.

Calling finish() in your activity before onResume() gets called (like the error message says) will stop this from happening.

My use case was launching an invisible Activity to handle deep links and direct it to the correct part of my app.




回答7:


Try changing targetSdkVersion to 22 in build.gradle. Theme.NoDisplay shows error in api level 23.



来源:https://stackoverflow.com/questions/32169303/activity-did-not-call-finish-api-23

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!