Zap*_*ica 5 android android-fragments android-dialog android-dialogfragment
我已经实现了一个 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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果显示对话框并且我旋转屏幕,则我的 dissmisslaodingDialog 不起作用。我认为这是因为当您旋转时,它会再次调用 on create 函数并丢失对对话框的引用,但我已将其设为静态。
上面的代码放在一个 Utility 类中,我从我的活动中调用它,如下所示:
util.showLoadingDialog("Loading", "");
SendCommand(deviceId, "Command");
Run Code Online (Sandbox Code Playgroud)
然后我调用utils.dismissLoadingDialogSendCommmand 的回调函数。
我在这里缺少什么?
{编辑}
我的活动片段中的代码
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.");
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
实用类:
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要限制对话框重新创建,您可以在对话框中重写 onCreate 方法:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) dismiss();
}
Run Code Online (Sandbox Code Playgroud)
取消DialogFragment屏幕旋转。我发现了一个几分钟前搜索和尝试所有解雇和处理旋转变化后是onPause方法DialogFragment在旋转过程中被调用,您可以关闭它在那里。
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;
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1762 次 |
| 最近记录: |