Android - Dealing with a Dialog on Screen Orientation change
- by Donal Rafferty
I am overriding the onCreateDialog and onPrepareDialog methods or the Dialog class.
I have followed the example from Reto Meier's Professional Android Application Development book, Chapter 5 to pull some XML data and then use a dialog to display the info.
I have basically followed it exactly but changed the variables to suit my own XML schema as follows:
@Override
public Dialog onCreateDialog(int id) {
switch(id) {
case (SETTINGS_DIALOG) :
LayoutInflater li = LayoutInflater.from(this);
View settingsDetailsView = li.inflate(R.layout.details, null);
AlertDialog.Builder settingsDialog = new AlertDialog.Builder(this);
settingsDialog.setTitle("Provisioned Settings");
settingsDialog.setView(settingsDetailsView);
return settingsDialog.create();
}
return null;
}
@Override
public void onPrepareDialog(int id, Dialog dialog) {
switch(id) {
case (SETTINGS_DIALOG) :
String afpunText = " ";
if(setting.getAddForPublicUserNames() == 1){
afpunText = "Yes";
}
else{
afpunText = "No";
}
String Text = "Login Settings: " + "\n"
+ "Password: " + setting.getPassword() + "\n"
+ "Server: " + setting.getServerAddress() + "\n";
AlertDialog settingsDialog = (AlertDialog)dialog;
settingsDialog.setTitle(setting.getUserName());
tv = (TextView)settingsDialog.findViewById(R.id.detailsTextView);
if (tv != null)
tv.setText(Text);
break;
}
}
It works fine until I try changing the screen orientation, When I do this onPrepareDialog gets call but I get null pointer exceptions on all my variables.
The error still occurs even when I tell my activity to ignore screen orientation in the manifest.
So I presume something has been left out of the example in the book do I need to override another method to save my variables in or something?