sumit pandey
sumit pandey

Reputation: 1221

how to view an alert dialog box even when android phone is rotated

I am creating an application in which an alert dialog box pops up when i click on a button.Now when the phone is rotated this view gets disappears.Can anyone help me to retain this alert view even if phone is rotated?

Upvotes: 1

Views: 1726

Answers (4)

rfsk2010
rfsk2010

Reputation: 8611

The activity restarts to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration. There might be a situation in which restarting the activity is not ideal, as in your case. You have two options

  1. Retain an object during a configuration change
  2. Handle the configuration change yourself

Handling the configuration change have been explained by fellow posters above. This should only be DONE, if your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.

Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a LAST RESORT when you must avoid restarts due to a configuration change and is not recommended for most applications

How I would handle your problem is, call Dialog.dismiss() from onDestroy() and re display it after the configuration has changed.

If you look at logcat, you would see that "Activity has leaked window that was originally added" , this is because Views have a reference to their parent Context (taken from constructor argument). If you leave an Activity without destroying Dialogs and other dynamically created Views, they still hold this reference to your Activity (if you created with this as Context: like new ProgressDialog(this)), so it cannot be collected by the GC, causing a memory leak.

Upvotes: 0

Dinesh Prajapati
Dinesh Prajapati

Reputation: 9510

put below code in the Activity where this dialog is popup

@Override
    public void onConfigurationChanged(Configuration newConfig) {


        super.onConfigurationChanged(newConfig);
    }

and

android:configChanges="orientation|keyboardHidden" this thing in the activity which is displaying the dialog

Upvotes: 0

Uday
Uday

Reputation: 6023

add android:configChanges="orientation|keyboardHidden" to your corresponding activity, this will resolve your issue.

Hope it helps.

Upvotes: 1

moujib
moujib

Reputation: 752

The matter is that the system destroys the activity when a change in the configuration occurs. See http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges

You have to add this to the activity declaration in the manifest:

android:configChanges="orientation"

so it looks like

<activity android:label="@string/app_name" 
android:configChanges="orientation|keyboardHidden" 
android:name=".your.package">

So putting that in the configuration file avoids the system to destroy your activity. Instead it invokes the onConfigurationChanged(Configuration) method.

Or you can "force" your activity to be fixed on landscape or portrait mode and then the alertview will be retained but the view will be fixed whether the phone rotates or not . It can be made by adding this to your manifest file (in your activity) :

android:screenOrientation="portrait" or android:screenOrientation="landscape"

Upvotes: 1

Related Questions