Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 508
Jeremy Logan Avatar asked Sep 17 '09 08:09

Jeremy Logan


2 Answers

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.

like image 52
Jeremy Logan Avatar answered Nov 03 '22 23:11

Jeremy Logan


Just a guess here...

I think @android:style/Theme.Dialog is set for much of the background to be translucent. Initially, your MainActivity's background is black. If the startActivityForResult() is kicking in before your MainActivity gets to draw, that might explain your problem.

Try using postDelayed() on a View to delay your startActivityForResult() by a few hundred milliseconds, and see if that changes the behavior.

like image 21
CommonsWare Avatar answered Nov 03 '22 23:11

CommonsWare