onActivityResult中的操作和“错误无法在onSaveInstanceState之后执行此操作”

时间:2012-08-24 07:38:01

标签: java android android-fragments

实现用户可以登录的应用程序我遇到以下情况:如果用户已登录,则执行操作,否则启动结果的登录活动,如果结果为Activity.RESULT_OK,则执行操作。

我的问题是,要执行的操作是显示DialogFragment,但调用

DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog")
onActivityResult回调中的

引发异常:

Caused by: java.lang.IllegalStateException:  
Can not perform this action after onSaveInstanceState

那我怎么解决这个问题呢?我想在那里举起一个标志并在onResume中显示对话框,但我看到这个解决方案有点脏了

修改:添加了更多代码(我按照此示例显示了DialogFragment

当用户请求操作时:

... 
if (!user.isLogged()){
 startActivityForResult(new Intent(cnt, Login.class), REQUEST_LOGIN_FOR_COMMENT);
}

在同一片段中

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_LOGIN_FOR_COMMENT && resultCode == Activity.RESULT_OK) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        DialogFragment newFragment = MyDialogFragment.newInstance();
        newFragment.show(ft, "dialog")
    }
}

如果用户登录Login活动呼叫;

setResult(Activity.RESULT_OK);
finish();

7 个答案:

答案 0 :(得分:97)

我想出的最好的事情是不要使用.show()而是这样做。

CheckinSuccessDialog dialog = new CheckinSuccessDialog();
//dialog.show(getSupportFragmentManager(), null);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(dialog, null);
ft.commitAllowingStateLoss();

答案 1 :(得分:10)

这是适用于我的解决方法。

private void alert(final String message) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        public void run() {
            AlertDialogFragment alertDialogFragment = AlertDialogFragment.newInstance(message);
            alertDialogFragment.show(getFragmentManager(), ALERT_DIALOG_FRAGMENT);
        }
    });        
}

答案 2 :(得分:4)

如果使用DialogFragment,唯一对我有用的就是用dissmissAllowingStateLoss()

来解散Fragment

答案 3 :(得分:4)

我只是检查一项活动是否被销毁。

if (!getActivity().isFinishing()) {
    DialogFragment fragment = MyFragment.newInstance();
    fragment.show(getActivity().getSupportFragmentManager(), MyFragment.TAG);
}

答案 4 :(得分:2)

覆盖show(Fragment manager, String tag)函数,允许状态丢失,并通过反射从原始函数更改mDismissed = false; mShownByMe = true;

public class DialogParent extends DialogFragment {

    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            Field mDismissed = DialogFragment.class.getDeclaredField("mDismissed");
            Field mShownByMe = DialogFragment.class.getDeclaredField("mShownByMe");
            mDismissed.setAccessible(true);
            mShownByMe.setAccessible(true);
            mDismissed.setBoolean(this, false);
            mShownByMe.setBoolean(this, true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }
}

答案 5 :(得分:0)

这是真的。

CheckinSuccessDialog dialog = new CheckinSuccessDialog();
//dialog.show(getSupportFragmentManager(), null);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(dialog, null);
ft.commitAllowingStateLoss();

但是仍然很糟糕,因为得到错误“活动已被破坏”

ava.lang.IllegalStateException: Activity has been destroyed fragmentTransaction.commitAllowingStateLoss();

所以我的解决方案是添加支票if (!isFinishing()&&!isDestroyed())

CheckinSuccessDialog fragment = CheckinSuccessDialog.newInstance();

     if (fragment instanceof DialogFragment) {
                DialogFragment dialog = (DialogFragment) fragment;
                if (!dialog.isAdded()) {
                    fragmentTransaction.add(dialog, 
                          CheckinSuccessDialog.class.getName());
                    if (!isFinishing()&&!isDestroyed()) {
                        fragmentTransaction.commitAllowingStateLoss();
                    }
                }

关于解雇:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(CheckinSuccessDialog.class.getName());
        if (fragment != null && fragment instanceof DialogFragment) {
            DialogFragment dialog = (DialogFragment) fragment;
            dialog.dismiss();
            if (!isFinishing()&&!isDestroyed()) {
                fragmentTransaction.commitAllowingStateLoss();
            }
        }

答案 6 :(得分:0)

FragmentTrasaction保存其状态后提交FragmentManager时,抛出此异常。简单明了的方法是在显示FragmentManager之前检查DialogFragment是否已保存状态。

if(!getSupportFragmentManager.isStateSaved()) {
    MyDialogFragment dialogFragment = new MyDialogFragment()
    dialogFragment.show(getSupportFragmentManager, TAG);
}