0

I have an Android app that begins by displaying a dialog. When I set the orientation to portrait and run on an Android 3.1 emulator, the dialog is displayed, but once the user dismisses it, it is displayed again. This doesn't happen on the 3.2 emulator or if the orientation isn't set. How do I prevent the dialog from being displayed twice on Android 3.1 with the orientation set to portrait?

Here's the code:

public class TestActivity extends Activity {    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        showDialog(0);
    }

    protected Dialog onCreateDialog(int id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Message.")
            .setPositiveButton("Ok.", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // nothing
                }
            });         
        return builder.create();
    }
}

Here is the manifest:

<uses-sdk android:minSdkVersion="7" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".TestActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

When my emulator opens, it's in landscape, if that matters.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
BenH
  • 2,100
  • 2
  • 22
  • 33

1 Answers1

3

I found this: android:configChanges. Hope it helps you :-)

E.g:

<activity
    ...
    android:configChanges="orientation|keyboardHidden"" />
  • Hmm...it works on my 2.2 emulator...I wonder what's different. Where do I put the `onDestroy` function you suggest? (I don't have a variable called `dialog`, so I wasn't sure if I needed something else too.) – BenH Mar 30 '12 at 01:11
  • @BenH You can put `onDestroy()` in your activity. Sorry I forgot you use `onCreateDialog()`, so you can use [dismissDialog(int)](http://developer.android.com/reference/android/app/Activity.html#dismissDialog(int)). –  Mar 30 '12 at 01:18