Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dialog dissappers after orientation changes. #24

Closed
isabsent opened this issue Apr 12, 2020 · 5 comments
Closed

Dialog dissappers after orientation changes. #24

isabsent opened this issue Apr 12, 2020 · 5 comments

Comments

@isabsent
Copy link

You have to use DialogFragment as a base class for your dialogs to avoid this problem.

@PatilShreyas
Copy link
Owner

Thanks @isabsent for this. I'll try to figure it out.

@isabsent
Copy link
Author

isabsent commented Apr 12, 2020

This dialog will alive after screen rotation and will keep all inputs if you add custom layouts into it:

public class RotationSafeDialog extends DialogFragment {
    private String message;
    private Activity act;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            act = (MainActivity) context;
        } catch (ClassCastException castException) {

        }
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            try {
                act = (MainActivity) activity;
            } catch (ClassCastException castException) {

            }
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        act = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        message = getArguments().getString(EXTRA_ARGS_MESSAGE);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog dialog = new AlertDialog.Builder(act)
                .setTitle("Title")
                .setMessage(message)
                .setNegativeButton(android.R.string.no, null)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }
                )
                .create();
        return dialog;
    }
}

Call it with

public void showRotationSafeDialog(String message){
    Bundle args = new Bundle();
    args.putString("EXTRA_ARGS_MESSAGE", message);
    RotationSafeDialog dialog = new RotationSafeDialog();
    dialog.setArguments(args);
    dialog.show(getSupportFragmentManager(), dialog.getClass().getName());
}

@PatilShreyas
Copy link
Owner

This should be managed from the Activity side I think. Whoever is creating dialog that should take care of that.

@isabsent
Copy link
Author

My code above is the way to take care about it.

@PatilShreyas
Copy link
Owner

Yes. I liked your suggestion. Will try to release next version with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants