0

I am new to Android and developing a utility UI component which displays dialog box on launch , I want to close the app if user does not make a choice in the dialog box and chooses to press back button. How do I achieve this in dialogFragment? I would not have access to the activity code .

This component is implemented as DialogFragment.

I tried

    dialog.setOnDismissListener( new OnDismissListener()
    {
        @Override
        public void onDismiss(DialogInterface dialog)
        {
            getActivity().finish();
        }
    });

but this does not close the activity if I press back button.

what is the best practice to implement mandatory dialog box in Android

Thanks in advance

user1411335
  • 3,139
  • 3
  • 18
  • 24
  • Is it a login dialog? I'm not sure why you wouldn't want the user to open up the application. You want to Override `onBackPressed()` http://stackoverflow.com/questions/18337536/android-overriding-onbackpressed – RoraΖ Aug 04 '14 at 18:38
  • Yes , this is a login dialogFragment. – user1411335 Aug 04 '14 at 18:46

1 Answers1

3
  1. Make a flag true when open the dialog...
  2. onBackPressed() check if the flag true...
  3. if true finish the activity...

I haven't tested but it may work ...

@Override
public void onDismiss(DialogInterface dialog)
{
            ((YourActivity)getActivity()).finish();
}

If the above one not working ... create a method in your activity for example ...

public void closeActivity(){

finish();

}

then call the function closeActivity() onDismiss

 @Override
    public void onDismiss(DialogInterface dialog)
    {
                ((YourActivity)getActivity()).closeActivity();
    }
Sabeer
  • 3,940
  • 1
  • 24
  • 20
  • Thanks for your reply , I don't have control over activity code , is there any way to achieve this through dialogFragment? – user1411335 Aug 04 '14 at 18:47