0

I have transparent activity after I kill it (onStop/onDestroy) i want to create Dialog but I getting error :

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

hole code looks like :

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");

    }
}

P.S Creating dialog works so there is no need to put here DialogChoideActivity code.

1 Answers1

0

You must allow state loss for dialog because you are using show during destruction. Use this

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

instead of dialog.show(getSupportFragmentManager(),"my_dialog");

Check also Demystifying Android’s commitAllowingStateLoss() for caveats using commitAllowingStateLoss.

Marco Righini
  • 506
  • 2
  • 12
  • I add your function and change my dialog.show on showDialogAllowingStateLoss(getSupportFragmentManager(),dialog,"my_dialog"); and now I'm getting error like : 'Unable to destroy activity : java.lang.IllegalStateException: Activity has been destroyed' –  May 10 '17 at 08:20
  • You can fix the problem starting TransparentTip through startActivityForResult(). Then you use show() in the onActivityResult callback of the calling Activity. – Marco Righini May 10 '17 at 09:54