cmackie21 cmackie21 - 3 months ago 18
Android Question

DialogFragment doesn't display unless phone is in landscape

I have a DialogFragment which displays if the user tries to do an action they have previously denied permissions for.

The DialogFragment code compiles fine but for reason it won't display the message in the Dialog unless the phone is turned landscape, after which the message stays.

Example of my problem

After turning the phone landscape, message displays[![][2]]3

Here is my Dialog Fragment code:

public class PermissionDialog extends DialogFragment {

private String title;
private String reason;
PermissionListener pListener;

public PermissionDialog() {
// Required empty public constructor

}

public interface PermissionListener{
//Interface listener so that activities which have dialogs can hear outcomes of events
void onDialogPositiveClick(DialogFragment dFragment);
void onDialogNegativeClick(DialogFragment dFragment);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
//Set up the elements in the dialog including listeners
AlertDialog.Builder permissDialog = new AlertDialog.Builder(getActivity());
if (savedInstanceState!=null){
Bundle args = getArguments();
title = args.getString("title","");
reason = args.getString("reason","");
}
permissDialog.setMessage(reason);
permissDialog.setTitle(title);
permissDialog.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
pListener.onDialogPositiveClick(PermissionDialog.this);
}
});
permissDialog.setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
pListener.onDialogNegativeClick(PermissionDialog.this);
}
});
return permissDialog.create();
}

@Override
public void onAttach(Activity activity){
//This method will ensure the activity implements the interface for sending events
super.onAttach(activity);
try {
pListener = (PermissionListener) activity;
}catch(ClassCastException e){
Toast.makeText(getActivity(), "Error in requesting permissions", Toast.LENGTH_SHORT).show();
}
}

}


This is the method where the fragment gets created:

private void checkStoragePermission() {
//This method will check whether the user has granted the permission to write their picture to external storage
if (Build.VERSION.SDK_INT<= Build.VERSION_CODES.LOLLIPOP_MR1){
//Users device runs Lollipop or lower so we do not need to check permissions
savePicture();
}else{
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
//Permission hasn't been granted - we should check if the user has denied this permission before
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Build a dialog to explain to user why permission is required
PermissionDialog storageDialog = new PermissionDialog();
Bundle args = new Bundle();
args.putString("title", "Approve Permission?");
args.putString("reason", "This is required in order to write graph to external storage.");
storageDialog.setArguments(args);
storageDialog.show(getSupportFragmentManager(),"storage_dialog");
}else{
//Request permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},WRITE_EXTERNAL_CODE);
}
}else if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) ==PackageManager.PERMISSION_GRANTED){
//Permission has been granted, we can save the picture.
savePicture();
}
}
}

Answer

I thinks savedInstanceState!=null is wrong. Are you intend to check savedInstanceState==null ?

Also I think this check is not necessary.

like this:

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    ....
    AlertDialog.Builder permissDialog = new AlertDialog.Builder(getActivity());
    Bundle args = getArguments();
    permissDialog.setMessage(args.getString("reason",""));
    permissDialog.setTitle(args.getString("title",""));
    ....
}

savedInstanceState is null at first onCreateDialog call. When you rotate device, DialogFragment is recreated by Android OS. And onCreateDialog is called with savedInstanceState != null.

Here is document.

savedInstanceState

Bundle: If non-null, this fragment is being re-constructed from a previous saved state as given here.

Comments