Search code examples
androidandroid-fragmentsandroid-dialogfragment

How to close Activity when DialogFragment is created?


Let me tell you all what I have done 'till now. I am showing a dialogfragment on device screen on a button click working perfectly. But now I want is to close activity on button click and show dialogfragment.

Code to open DialogFragment on button click :

button1.setOnClickListener(new OnClickListener() {
@Override
    public void onClick(View v) {
        Bundle bundle=new Bundle();
        bundle.putString("dialogData", data);

        FragmentManager fm = getFragmentManager();
        BuyInfoDilaog dialogFragment = new BuyInfoDilaog();
        dialogFragment.setArguments(bundle);
        dialogFragment.show(fm, "Please Enter your Details");
  }
});

How to deal with this case, does anyone have any idea? Please help. Thanks!


Solution

  • You won't be able to do so. The fragment lifecycle is linked with your activity's one. If you finish the activity, it will destroy the fragment. See :

    enter image description here

    You can pass the infos to construct your dialog to your previous activity using startActivityForResult and create the dialog then.