在onActivityResult和动作]错误不能执行的onSaveInstanceState&QUOT后,这个动作; [英] Actions in onActivityResult and "Error Can not perform this action after onSaveInstanceState"

查看:355
本文介绍了在onActivityResult和动作]错误不能执行的onSaveInstanceState&QUOT后,这个动作;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现一个应用程序,用户可以登录我有以下情况:如果用户登录进行操作否则启动登录活动的结果,如果结果是Activity.RESULT_OK做动作

我的问题是,它进行的动作是显示DialogFragment,但调用

  DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(英尺,对话)
 

在onActivityResult回调抛出一个异常:

 产生的原因:java.lang.IllegalStateException:
之后的onSaveInstanceState无法执行此操作
 

所以,我怎么能解决这个问题?我想在提高的标志出现,并显示在onResume对话,但我看到这个解决方案有点脏

编辑:增加了更多的code(IM下面这个例子显示了 DialogFragment

当被由用户请求的操作:

  ...
如果(!user.isLogged()){
 startActivityForResult(新意图(CNT,Login.class),REQUEST_LOGIN_FOR_COMMENT);
}
 

在同一片段

  @覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,因此code,数据);
    如果(要求code == REQUEST_LOGIN_FOR_COMMENT和放大器;&安培;结果code == Activity.RESULT_OK){
        FragmentTransaction英尺= getFragmentManager()的BeginTransaction()。
        DialogFragment newFragment = MyDialogFragment.newInstance();
        newFragment.show(英尺,对话)
    }
}
 

如果在登录活动的用户登录调用;

 的setResult(Activity.RESULT_OK);
完();
 

解决方案

最好的事情我已经出来是不使用.show(),而是做到这一点。

  CheckinSuccessDialog对话框=新CheckinSuccessDialog();
//dialog.show(getSupportFragmentManager(),NULL);
FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
ft.add(对话框,NULL);
ft.commitAllowingStateLoss();
 

Implementing an app where the user can log in I have the following situation: If the user is logged in perform the action else start the login activity for result and if the result is Activity.RESULT_OK do the action.

My problem is that the action to perfom is to show a DialogFragment, but calling

DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog")

in the onActivityResult callback throws an exception:

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

So how can I solve this? I'm thinking in raising a flag there and show the dialog in the onResume but I see this solution a little dirty

Edit: Added more code (Im following this example for showing the DialogFragment

When the action is requested by the user:

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

In the same fragment

@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")
    }
}

And if the user logs in the Login activity calls;

setResult(Activity.RESULT_OK);
finish();

解决方案

Best thing I've come up with is to not use .show() but rather do this.

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

这篇关于在onActivityResult和动作]错误不能执行的onSaveInstanceState&QUOT后,这个动作;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆