4

So I am not a very experience Android programmer, so please be gentle with me :)

I am trying to create an app that uses fragements and from within one of these fragments I am calling a custom dialog box.

//create dialog
final Dialog dialog = new Dialog(getActivity());
dialog.setCancelable(false);
dialog.setContentView(R.layout.fragment_update_dialog);

//set up data in dialog here

Button bUpdate = (Button) dialog.findViewById(R.id.bDialogUpdate);
bUpdate.setOnClickListener(new OnClickListener() 
{
//define onclick listener code here
});
dialog.show();

This code works fine and I have no issues with it. But when there is a screen orientation change then my dialog box disappears.

Now I have read several posts on this forum, and other places, so I understand why this is occuring, but I don't find a solution to stop it.

I have tried the 'trick' with the manifest file, but it doesn't work. (Perhaps because it is in the fragment and not the activity?)

My manifest file includes;

<activity
        android:name="com.mycompany.myapp.MainActivity"
        android:configChanges="keyboardHidden|orientation"
....

and in my main activity I have

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
}

But this doesn't work.

I also see many comments say that this is not recommened, but can't see how to solve this issue.

Thanks for your help

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51

3 Answers3

13

Try to use DialogFragment instead. It restarts after orientation change. You need to extend this class as shown on documentation, and use it to show dialog.

bendaf
  • 2,981
  • 5
  • 27
  • 62
Yoric
  • 499
  • 3
  • 7
  • I looked at doing this, but had other issues. That being that I couldn't call the DialogFragment from inside another fragement. All the examples I found on the net where calling from within the activity, and I couldn't even get the code to compile. – jason.kaisersmith May 17 '13 at 10:19
  • @jason.kaisersmith There is example how I show DialogFragment from another fragment: `CustomDialogFragment dialog = new CustomDialogFragment(); dialog.show(getFragmentManager(), "tag");` You can find how to create custom DialogFragment in official documentation on link what i provided above. – Yoric May 17 '13 at 15:01
  • 3
    DialogFragment is the way to go. No need to make unrecommended changes to the manifest. – dennisdrew Jun 20 '14 at 21:36
10

Up to API 13 there was a new value to the configChanges attribute, screenSize

So if you're using large screens make sure to add screenSize in your configChanges attribute:

android:configChanges="orientation|keyboardHidden|screenSize"

i.e.

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

Reference

Community
  • 1
  • 1
Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
  • Thats solved the disappearing dialog (brilliant thanks), but has created another issue! Now, the dialog box stays, I can perform my update. But when it returns to the fragment then my fragment view has disappeared, and all of my content (textboxs, spinners etc) have gone. So what have I done wrong now? – jason.kaisersmith May 17 '13 at 10:00
0

By accident i found a way to make your Dialog persist when device orientation changes.

This is C# xamarin code but i believe it can be very easily adapted to java as well.

    private void MakeDialogPersist(Dialog dialog)
    {
        WindowManagerLayoutParams wmlp = new WindowManagerLayoutParams();
        wmlp.CopyFrom(dialog.Window.Attributes);
        dialog.Window.Attributes = wmlp;
    }

I run this method after i perform dialog.Show();

SubqueryCrunch
  • 1,325
  • 11
  • 17