Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

android - Dialog box disappears during orientation change in fragment

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...