Failure delivering result ResultInfo | java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

China☆狼群 提交于 2019-12-10 10:28:20

问题


I have Simple app first I display MainActivity then after MainActivity became visible I display TransparentActivity after that onClick I kill TransparentActivity and I create and display dialog. During last step I get Error

Error

Failure delivering result ResultInfo{who=null, request=1234, result=-1, data=Intent {  }} to activity {com.example.kylu.layout/com.example.kylu.layout.GuidePhotoAlbum}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

MainActivity

public class MainActivity extends FragmentActivity
{
public static boolean flag=false;
    private static int REQUEST_CODE = 1234;

    @Override public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
    }

@Override
protected void onResume() {
    super.onResume();
    //transparentShows only once
    if(!flag)
    {
        Intent i = new Intent(this, TransparentTip.class);
        startActivityForResult(i, REQUEST_CODE);
        flag=true;
    }
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (REQUEST_CODE == requestCode) {
            if (resultCode == Activity.RESULT_OK) {
                Toast.makeText(this, "Show dialog", Toast.LENGTH_SHORT).show();
                DialogChoiceActivity dialogChoiceActivity = new DialogChoiceActivity();
                dialogChoiceActivity.show(getSupportFragmentManager(),"mydialog");
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Toast.makeText(this, "Don't show dialog", Toast.LENGTH_SHORT).show();
            }
        }
    }

TransparentTip

    public class TransparentTip extends Activity {

        public static int countTransp=0;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.transparent_tip);
            Button ok=(Button)findViewById(R.id.bToK);
        }

        public void buttonClick(View view)
        {
            if (view.getId() == R.id.bToK)
            {
                final Intent intent = new Intent();
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        }

DialogChoiceActivity

public class DialogChoiceActivity extends DialogFragment {

    LayoutInflater inflater;
    View v;

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        inflater = getActivity().getLayoutInflater();
        v = inflater.inflate(R.layout.dialog_email, null);
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Email " + " 1/10");
        builder..setNegativeButton("Exit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });
        return builder.create();
    }
}

After Transparent is no longer visible I want to display Dialog above MainActivity. I tryied display this dialog onResume to but I get similar error


回答1:


It should work if you put this line into your onActivityResult:

super.onActivityResult(requestCode,resultCode,intent);

So, your onActivityResult should look like this:

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode,resultCode,intent);
    if (REQUEST_CODE == requestCode) {
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(this, "Show dialog", Toast.LENGTH_SHORT).show();
            DialogChoiceActivity dialogChoiceActivity = new DialogChoiceActivity();
            dialogChoiceActivity.show(getSupportFragmentManager(),"mydialog");
        } else if (resultCode == Activity.RESULT_CANCELED) {
            Toast.makeText(this, "Don't show dialog", Toast.LENGTH_SHORT).show();
        }
    }
}



回答2:


You can use dialogChoiceActivity.show(getFragmentManager(),"mydia‌​log");

this line has syntax error there is two dots builder..setNegativeButton("Exit", new DialogInterface.OnClickListener() {

should be like this builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {



来源:https://stackoverflow.com/questions/43935681/failure-delivering-result-resultinfo-java-lang-illegalstateexception-can-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!