问题
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(),"mydialog");
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