快速单击按钮后无法关闭 DialogFragment (Android)
Can't dismiss DialogFragment after clicking the button very quickly (Android)
我有一个 DialogFragment
,上面有一个简单的 cancel
按钮。
我想要做的是每次按下取消按钮后关闭对话框。
但是,我发现如果在对话框打开后非常快速地单击按钮,
dismiss()
方法似乎不起作用,对话框会卡在那里……永远……
我的代码(详细信息已简化):
- 1.Custom 扩展
DialogFragment
的 DialogFragment
public class MyDialogFragment extends DialogFragment {
private Callback callback;
// An interface for implementing functions triggered by onClick event.
public interface Callback{
void onCancelClick();
}
// onClick event injection using butterKnife
@OnClick(R.id.button_cancel)
void onClick(View view) {
callback.onCancelClick();
}
// create a new instance
public static MyDialogFragment newInstance() {
MyDialogFragment fragment = new MyDialogFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
/**
Lifecycle begins
*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Callback) {
callback = (Callback) context;
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_dialog_fragment, container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Dialog dialog = getDialog();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
}
}
- 主持人activity.
public class MyActivity extends AppCompatActivity implements
MyDialogFragment.Callback {
private MyDialogFragment myDialogFragment;
private boolean isTaskRunnable = false;
/**
* Call some API
*/
private void callMyApi() {
// Create and show the dialog represents data loading.
myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(getSupportFragmentManager(), "myDialogFragment");
TaskRunnable taskRunnable = new TaskRunnable<XXXXXX>() {
@Override
public responseList doLongOperation(params) throws MyException {
isTaskRunnable = true;
//...... asynchronous execution
return //....
}
@Override
public void callback(responseList response) {
super.callback(response);
if (isTaskRunnable) {
isTaskRunnable = false;
// dismiss the dialog (worked)
if (myDialogFragment != null) {
myDialogFragment.dismiss();
}
}
@Override
public void onFailure(AsyncStatus.ErrorReason reason, String msg) {
super.onFailure(reason, msg);
if (isTaskRunnable) {
isTaskRunnable = false;
// dismiss the dialog (worked)
if (myDialogFragment != null) {
myDialogFragment.dismiss();
}
}
}
};
taskRunnable.setParams(params);
AsyncManager.runBackgroundTask(taskRunnable);
}
/**
* Dismiss() sometimes doesn't work
* when I press the button immediately after openning the dialog.
*/
@Override
public void onCancelClick() {
isTaskRunnable = false;
AsyncManager.cancelAllTasks();
myDialogFragment.dismiss();
}
}
如果有人能帮我指出问题或给我一些线索,我将不胜感激。
不是来回答你的问题,而是发现了一些奇怪的东西。
myDialogFragment = myDialogFragment.newInstance();
应该是 myDialogFragment = MyDialogFragment.newInstance();
吗?
我知道这不是解决方案,而是非常便宜的解决方法,但是当我遇到与时间相关的问题时,我只是将导致问题的代码放在 Handler().postDelayed(problemedFunc, 500) 中,就像那样
我有一个 DialogFragment
,上面有一个简单的 cancel
按钮。
我想要做的是每次按下取消按钮后关闭对话框。
但是,我发现如果在对话框打开后非常快速地单击按钮,
dismiss()
方法似乎不起作用,对话框会卡在那里……永远……
我的代码(详细信息已简化):
- 1.Custom 扩展
DialogFragment
的 DialogFragment
public class MyDialogFragment extends DialogFragment {
private Callback callback;
// An interface for implementing functions triggered by onClick event.
public interface Callback{
void onCancelClick();
}
// onClick event injection using butterKnife
@OnClick(R.id.button_cancel)
void onClick(View view) {
callback.onCancelClick();
}
// create a new instance
public static MyDialogFragment newInstance() {
MyDialogFragment fragment = new MyDialogFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
/**
Lifecycle begins
*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Callback) {
callback = (Callback) context;
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_dialog_fragment, container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Dialog dialog = getDialog();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
}
}
- 主持人activity.
public class MyActivity extends AppCompatActivity implements
MyDialogFragment.Callback {
private MyDialogFragment myDialogFragment;
private boolean isTaskRunnable = false;
/**
* Call some API
*/
private void callMyApi() {
// Create and show the dialog represents data loading.
myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(getSupportFragmentManager(), "myDialogFragment");
TaskRunnable taskRunnable = new TaskRunnable<XXXXXX>() {
@Override
public responseList doLongOperation(params) throws MyException {
isTaskRunnable = true;
//...... asynchronous execution
return //....
}
@Override
public void callback(responseList response) {
super.callback(response);
if (isTaskRunnable) {
isTaskRunnable = false;
// dismiss the dialog (worked)
if (myDialogFragment != null) {
myDialogFragment.dismiss();
}
}
@Override
public void onFailure(AsyncStatus.ErrorReason reason, String msg) {
super.onFailure(reason, msg);
if (isTaskRunnable) {
isTaskRunnable = false;
// dismiss the dialog (worked)
if (myDialogFragment != null) {
myDialogFragment.dismiss();
}
}
}
};
taskRunnable.setParams(params);
AsyncManager.runBackgroundTask(taskRunnable);
}
/**
* Dismiss() sometimes doesn't work
* when I press the button immediately after openning the dialog.
*/
@Override
public void onCancelClick() {
isTaskRunnable = false;
AsyncManager.cancelAllTasks();
myDialogFragment.dismiss();
}
}
如果有人能帮我指出问题或给我一些线索,我将不胜感激。
不是来回答你的问题,而是发现了一些奇怪的东西。
myDialogFragment = myDialogFragment.newInstance();
应该是 myDialogFragment = MyDialogFragment.newInstance();
吗?
我知道这不是解决方案,而是非常便宜的解决方法,但是当我遇到与时间相关的问题时,我只是将导致问题的代码放在 Handler().postDelayed(problemedFunc, 500) 中,就像那样