我有一个处理用户注册的活动(FragmentActivity)。在按钮上单击时,将显示DialogFragment,同时在活动中异步处理任务。然后在处理完任务后,FragmentDialog被解除。一切顺利,即使配置更改,如更改方向,但我发现如果我在后台任务运行时按下主页按钮,应用程序崩溃在它试图解除FragmentDialog的点,当我重新启动应用程序,我发现对话框仍在运行。
我搜索并找到了大量讨论,解决方案和解决方法,here,here和here。但是我无法实现其中任何一个来解决我的问题。
以下是我正在使用的内容:
按下按钮点击注册用户的方法:
private void startRegDialog(final String phoneNumber, String password) {
Log.i("tag", "startregdailog called");
verifiedUser = false;
//start Dialog fragment here.
final SignUpDialogFragment mDialogFragment = SignUpDialogFragment.newInstance();
mDialogFragment.show(getSupportFragmentManager(), "dailogFragment");
ParseUser CreateNewUser = new ParseUser();
CreateNewUser.setUsername(phoneNumber);
CreateNewUser.setPassword(password);
CreateNewUser.put("userVerified", verifiedUser);
Log.i("tag", "starting task");
CreateNewUser.signUpInBackground(new SignUpCallback() {
@Override
public void done(ParseException e) {
Log.i("tag", "task done");
//Dismiss Dialog after receiving result
if(mDialogFragment !=null){
mDialogFragment.dismiss();
}
if(e != null){
Log.i("Tag", "user not Created");
String errorMessage = e.getMessage().toString();
onError(errorMessage);
}
else {
Log.i("Tag", "userSuccessfully Created");
Intent VerifyAccount = new Intent(context, VerifyAccount.class);
VerifyAccount.putExtra("phoneNo", phoneNumber);
startActivity(VerifyAccount);
}
}
});
}
这是LOGCAT输出
03-17 22:12:40.070: E/AndroidRuntime(16000): FATAL EXCEPTION: main
03-17 22:12:40.070: E/AndroidRuntime(16000): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:189)
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:155)
03-17 22:12:40.070: E/AndroidRuntime(16000): at com.example.razcat.CreateAccount$3.done(CreateAccount.java:273)
03-17 22:12:40.070: E/AndroidRuntime(16000): at com.parse.SignUpCallback.internalDone(SignUpCallback.java:39)
03-17 22:12:40.070: E/AndroidRuntime(16000): at com.parse.SignUpCallback.internalDone(SignUpCallback.java:1)
03-17 22:12:40.070: E/AndroidRuntime(16000): at com.parse.Parse$6$1.run(Parse.java:834)
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.os.Handler.handleCallback(Handler.java:615)
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.os.Looper.loop(Looper.java:155)
03-17 22:12:40.070: E/AndroidRuntime(16000): at android.app.ActivityThread.main(ActivityThread.java:5454)
03-17 22:12:40.070: E/AndroidRuntime(16000): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 22:12:40.070: E/AndroidRuntime(16000): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 22:12:40.070: E/AndroidRuntime(16000): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
03-17 22:12:40.070: E/AndroidRuntime(16000): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
03-17 22:12:40.070: E/AndroidRuntime(16000): at dalvik.system.NativeStart.main(Native Method)
Please any help would be useful thanks.
答案 0 :(得分:0)
要删除Dialog Fragment,您需要通过它标记来调用它,然后才能解除它。在你的情况下,你已经采取了" dailogFragment"作为标签。
mDialogFragment.show(getSupportFragmentManager(), "dailogFragment");
所以现在要删除它,首先通过它的标签调用它,如下所示:
Fragment mDialogFragment= getSupportFragmentManager().findFragmentByTag("dailogFragment");
//then check if it's null
if (mDialogFragment!= null) {
DialogFragment df = (DialogFragment) mDialogFragment;
//Now dismiss.
df.dismiss();
}