0

杀死它后我有透明的活动(onStop/onDestroy)我想创建对话框但我收到错误:

java.lang.RuntimeException: Unable to destroy activity {package name/myclass}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

孔代码如下所示:

public class TransparentTip extends FragmentActivity {

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

    public void buttonClick(View view) {
        if (view.getId() == R.id.bToK)
        {
            finish();
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        DialogChoiceActivity dialog = new DialogChoiceActivity();
        dialog.show(getSupportFragmentManager(),"my_dialog");

    }
}

PS 创建对话框有效,因此无需在此处放置 DialogChoideActivity 代码。

4

1 回答 1

0

您必须允许对话框的状态丢失,因为您在销毁期间使用 show。用这个

public static void showDialogAllowingStateLoss(FragmentManager fragmentManager, DialogFragment dialogFragment, String tag) {
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.add(dialogFragment, tag);
    ft.commitAllowingStateLoss();
}

代替dialog.show(getSupportFragmentManager(),"my_dialog");

还要检查Demystifying Android 的 commitAllowingStateLoss()以了解使用 commitAllowingStateLoss 的警告。

于 2017-05-10T08:13:57.457 回答