Search code examples
androidandroid-themeandroid-dialog

Solid black screens when calling Dialogs in onCreate()


I've had this problem in a few different apps now and I can't seem to find a solution.

If, in the onCreate() of an Activity, I start an activity that uses the dialog theme it doesn't draw anything to screen... the whole screen stays black. All the views are there (e.g., I can tap where an EditText should be and it'll give me the keyboard), they're just not visible.

Stupid simple example, for fun:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);
        startActivityForResult(new Intent(this, CredentialsInputActivity.class), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // do some crap with the result, doesn't really matter what
    }
}

CredentialsInputActivity is pretty straight forward... just extends Activity and has the theme set to @android:style/Theme.Dialog in the manifest file.


Solution

  • It turns out that this is a known bug in 1.5 (fixed in 1.6 and never a problem in 1.1). The bug stems from the animation for the new Activity taking place before the old Activity has been drawn, but it only presents if the "old" Activity was the first Activity in the Task.

    A workaround is to disable the animation for the theme. The simplest way to do this it with a new theme that extends the main dialog theme.

    res/values/themes.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="CupcakeDialog" parent="android:Theme.Dialog">
            <item name="android:windowAnimationStyle">@null</item>
        </style>
    </resources>
    

    Then just reference it in your AndroidManifest.xml:

    <!-- ... -->
    <activity 
        android:name=".CredentialsInputActivity"
        android:label="@string/CredentialsInputActivity_window_title"
        android:theme="@style/CupcakeDialog" />
    <!-- ... -->
    

    Obviously, you loose the animation, but at least you can see it :)

    Note: commonsware.com's solution worked fine too with the caveat I noted in the comments.