I'm currently having issues with DialogFragments. I use the latest support packages v.4 (option 8, I think) My problem is that if the orientation of my phone changes when the dialog is open, the application starts acting strange.
Currently my application works as follows: There is a FragmentActivity, it calls a fragment. This snippet then calls DialogFragment (via getActivity (). GetSupportFragmentManager ().
If the orientation changes when the dialog opens, getActivity () at Fragment = null. This causes a problem if I want to end an Activity, etc.
To call this, you open a dialog box, change the orientation and click the button. Only after pressing the button does the crash
My DialogFragment is called AlertDialogFragment:
public class AlertDialogFragment extends DialogFragment {
private static Builder mBuilder;
private static DialogInterface.OnClickListener mListener;
public static AlertDialogFragment newInstance(Context context, DialogInterface.OnClickListener listener) {
mBuilder = new AlertDialog.Builder(context);
mListener = listener;
return new AlertDialogFragment();
}
public void setButton(int whichButton, CharSequence buttonText) {
final DialogInterface.OnClickListener listener = mListener == null ? null : mListener;
mBuilder.setPositiveButton(buttonText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
listener.onClick(dialog, whichButton);
}
});
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return mBuilder.create();
}
}
This is a snippet:
public class TestBedFragment extends Fragment implements DialogInterface.OnClickListener {
private void showCrashDialog() {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
AlertDialogFragment newDialog = AlertDialogFragment.newInstance(getActivity(), this);
newDialog.setTitle("Test");
newDialog.setIcon(android.R.drawable.ic_dialog_alert);
newDialog.setMessage("Testing testing testing... 1, 2, 3... Just press Ok.");
newDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Ok");
newDialog.show(ft, "dialog");
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("TestBedFragment", "this = " + this);
Log.e("TestBedFragment", "getActivity() = " + getActivity());
getActivity().finish();
}
}
I'm not too sure what causes this? Sorry if this is a stupid question. I read elsewhere about "Windows Leaking", but I did not see any mention of this in logcat.
Can someone help me :) He is really appreciated
thank