我有一个DialogFragment,显示用户是否尝试执行之前已拒绝其权限的操作。
DialogFragment代码编译正常,但由于理由它不会在Dialog中显示消息,除非手机转为横向,之后消息仍然存在。
] 3
这是我的Dialog Fragment代码:
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();
}
}
}
这是创建片段的方法:
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();
}
}
}
答案 0 :(得分:2)
我认为savedInstanceState!=null
错了。
您打算查看savedInstanceState==null
吗?
此外,我认为此检查不是必需的。
像这样:@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
来电时, onCreateDialog
为空。旋转设备时,Android操作系统会重新创建DialogFragment
。使用onCreateDialog
调用savedInstanceState != null
。
Here是文档。
<强> savedInstanceState 强>
Bundle:如果非null,则从此处给出的先前保存状态重新构造此片段。