0

I have a dialog fragment that shows while my application is doing a task.

I cancel the task that is being executed on the activities onDestroy() method is called.

If the user rotates while the popup is showing, android destroys the activity and then redraws it. So I have told it to cancel the task in the onDestroy() method. It then stops executing the task and the popup stays there. As it is normally only removed when the task has completed.

I know this is probably not the best way to go about doing it, but I feel it will be a quick fix to my issue and it wont cause any other issues (that I know of).

I want to disable rotation for my application just before the popup is shown, and then I want to re-enable it when the dialog is dimsised.

How would I do this programtically?

like so: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); ?

And where would I do it? In the onCreate() of the dialog? or before I call the dialog from the activity?

Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • What about trying this instead: http://stackoverflow.com/questions/16605952/dialog-box-disappears-during-orientation-change-in-fragment – Byron May 19 '14 at 11:16

2 Answers2

0

Try to do this, and tell your experience! Just when you call your implementation of onDestroy() use

onDestroy(){
    OrientationListener foo = new OrientationListener(this.getContext());
    foo.disable();
}

I think that if this doesnt work is close to. Anyway you should take a look at this.

I hope this will help you!

Pahgo
  • 114
  • 6
  • The type OrientationListener is deprecated, will this not cause me any issues down the line? – Zapnologica May 19 '14 at 11:35
  • Oops, my fault, use [OrientationEventListener](http://developer.android.com/reference/android/view/OrientationListener.html) instead. And I'm not sure this works the way you want. I should have to try it... Anyway someone posted something interesting above – Pahgo May 19 '14 at 11:40
0

try to implement onDismiss and onShow listeners for your dialog and handle orientation. for example do something like this:

public class MyActivity extends Activity
    implements DialogInterface.OnDismissListener,DialogInterface.OnShowListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AlertDialog alertDialog = new 
        AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Alert message to be shown");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialog.setOnShowListener(this);
        alertDialog.setOnDismissListener(this);
        alertDialog.show();
    }
    @Override
    public void onShow(DialogInterface dialog) {
        final int screenOrientation = ((WindowManager)  getbaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
        switch (screenOrientation){
        case Surface.ROTATION_180:
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            break;
        case Surface.ROTATION_270:
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            break;
        case Surface.ROTATION_0:
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        case Surface.ROTATION_90:
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;
    }
    }
    @Override
    public void onDismiss(DialogInterface dialog) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
    }
}  

I hope this helps some one.