我已经实现了一个显示简单消息的 dialogFragment:
private static DialogFragment loadingDialogFragment = null;
public void showLoadingDialog(String title, String message) {
loadingDialogFragment = new LoadingDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
loadingDialogFragment.setArguments(args);
loadingDialogFragment.show(fragManager, "loadingDialog");
}
public void dismissLoadingDialog() {
if (loadingDialogFragment != null) {
loadingDialogFragment.dismiss();
}
}
但是,如果显示对话并且我旋转了屏幕,那么我的 dissmisslaodingDialog 将不起作用。我认为这是因为当您旋转时它会再次调用 on create 函数并且它会丢失对对话框的引用,但我已将其设为静态。
上面的代码放在我从我的 Activity 中调用的实用程序类中,如下所示:
util.showLoadingDialog("Loading", "");
SendCommand(deviceId, "Command");
然后我在 SendCommmand 的回调函数中调用 utils.dismissLoadingDialog
。
我在这里错过了什么?
{编辑}
我的activityFragment中的代码
util.showLoadingDialog("Loading", "");
SendCommand(deviceId, "Command");
public void SendCommand(int deviceId, final String command) {
Network.get(mContext, "/" + command + "/" + deviceId, null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.i(TAG + " sendPanelCommand", "recieved ok response to command: " + command);
util.dismissLoadingDialog();
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
util.dismissLoadingDialog();
util.showErrorDialog(
"Error",
"An error has occored while trying to issue the command to the panel. This can be caused by network issues or your panel sending out priority information such as alarms. Please try again.");
}
});
}
}
实用程序类:
public class Utils {
private Context context;
private FragmentManager fragManager;
public Utils(Context context) {
this.context = context;
}
public Utils(Context context, FragmentManager fragman) {
this.context = context;
this.fragManager = fragman;
}
...
private DialogFragment loadingDialogFragment = null;
public void showLoadingDialog(String title, String message) {
loadingDialogFragment = new LoadingDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
loadingDialogFragment.setArguments(args);
loadingDialogFragment.show(fragManager, "loadingDialog");
}
public void dismissLoadingDialog() {
if (loadingDialogFragment != null) {
loadingDialogFragment.dismiss();
} else {
// the reference is null, try the FragmentManager to see if it
// wasn't
// automatically restored
DialogFragment dg = (DialogFragment) fragManager.findFragmentByTag("loadingDialog");
if (dg != null) {
// this reference isn't null so the dialog is available
dg.dismiss();
}
}
}
}
最佳答案
在屏幕旋转时取消 DialogFragment
。几分钟前,在搜索并尝试了所有用于关闭和处理旋转的变体之后,我发现 DialogFragment
的 onPause
方法在旋转期间被调用,您可以关闭它在那里。
public static class ErrorDialogFragment extends DialogFragment {
// Global field to contain the error dialog
private Dialog mDialog;
public void onPause(){
super.onPause();
this.dismissAllowingStateLoss();
}
static ErrorDialogFragment newInstance() {
ErrorDialogFragment d = new ErrorDialogFragment();
return d;
}
...
}
关于android - 如何在旋转屏幕后关闭 dialogfragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23384929/