tags:

views:

506

answers:

2

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.

Anyone have any ideas?

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.

+1  A: 

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.

CommonsWare
I thought about this (not having had an opportunity to draw), but decided I didn't like it because shouldn't the new activity still be visible, even if only barely? Anyhow... I'll try it out tomorrow and see what happens.
fiXedd
I would have expected the setContentView() to be handled before startActivityForResult(). However, the ways of Android are mysterious, and so based on your experience, my guess is that startActivityForResult() gets processed first.
CommonsWare
This worked fine (in that "I hate hacks" sort of way ;) )... it'd be nice if the Activity had an onDisplayed() or something so we could tell when it is visible. NOTE: Doing it on a View inside of the first Activity (MainActivity above) made it so that every time it was drawn the ondraw() would run... sounds like what you want, but an unexpected side-effect (for me) was that if you open the onscreen keyboard on the sub-activity the parent activity gets drawn again giving you another popup. Just something to keep in mind when coding it up.
fiXedd
+3  A: 

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.

fiXedd
The bug does not seem to be fixed in 1.6, as far as I can tell...
polyglot