34

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.

Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
olfek
  • 3,210
  • 4
  • 33
  • 49

8 Answers8

26

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.

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
TechAurelian
  • 5,561
  • 5
  • 50
  • 65
  • 14
    I recommend `Theme.Translucent.NoTitleBar`. Otherwise, you'll get an old-style title bar floating on the screen. Otherwise, this technique worked for me -- many thanks! – CommonsWare Nov 01 '15 at 21:50
  • 3
    It looks like that is not a bug. Documentation at http://developer.android.com/reference/android/R.attr.html#windowNoDisplay says that "activity must immediately quit without waiting for user interaction". So as exception message suggests Activity needs to be finished before `onResume()` completes. – koral Nov 04 '15 at 19:08
22

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

@Override
protected void onStart() {
    super.onStart();
    setVisible(true);
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Sam
  • 40,644
  • 36
  • 176
  • 219
  • 1
    This does not work, at least as of Android 6.0 final. I still get the same exception as cited in the question. – CommonsWare Nov 01 '15 at 21:38
  • @CommonsWare, sounds like they fixed it in the final version. The fix was working in the official emulator before the official 6 release. Thanks for letting me know. – Sam Nov 02 '15 at 03:53
  • 3
    My apologies. I think I tried `setVisible(false)`. `setVisible(true)` avoids the crash, but you wind up showing a useless activity UI (black background, old-style gray title bar). `Theme.Translucent.NoTitleBar` is closer to the visual results of `Theme.NoDisplay` (i.e., invisible). – CommonsWare Nov 02 '15 at 12:43
15

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

Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
  • 2
    1. broken link 2. this seems to be an intended change in behavior starting with api 23, not a bug, below answers can help, and this post to summarize it: https://commonsware.com/blog/2015/11/02/psa-android-6p0-theme.nodisplay-regression.html – marmor Jul 20 '16 at 09:48
  • Link fixed. This is a bug recognized by Google developers. – Malwinder Singh Aug 12 '16 at 17:42
  • 1
    This error still happens in Android 9.0, so I think it's safe to say that it's here to stay. – Sam Mar 11 '19 at 00:18
6

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>
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • As my edit got rejected for some reason, answer is missing `@android:color/transparent` line to hide the statusbar when Activity is closed. – Ako May 09 '20 at 14:33
  • @Ako I added the line though it needs a separate `styles.xml` for api 21+ – M D P Jul 08 '20 at 22:10
  • This answer already needs a app specific styles.xml included in the project. – Ako Aug 12 '20 at 23:04
5

I have found the actual intended fix for this. The Theme.NoDisplay is only for

activities that don't actually display a UI; that is, they finish themselves before being resumed.

Source: Android Dev

Therefore, if you assign this theme to an activity you must call finish() before exiting onCreate() (i.e. before the system calls onResume) This is why the error says "did not call finish() prior to onResume()".

So the actual usecase for this theme is bootstrap activities and the like, for example like this when you have to determine whether you show the login or main view depending on whether the user is already authenticated:

public class BootstrapActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!userLoggedIn) {
            startActivity(new Intent(this, LoginActivity.class));
        } else {
            startActivity(new Intent(this, MainActivity.class));
        }
        finish();
    }

}
Twometer
  • 1,561
  • 2
  • 16
  • 24
1

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))}
KenHBS
  • 6,756
  • 6
  • 37
  • 52
0

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.

StuStirling
  • 15,601
  • 23
  • 93
  • 150
-4

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

k8C
  • 414
  • 4
  • 9
  • 1
    changing target sdk version is not a soltution. Google has changed its policy to upgrade the target sdk to latest before updating or creating new app on playstore – Ahmad Ayyaz Jun 18 '20 at 05:46