Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.1 kB
1
Indexable
Never
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Check if the dialog was retained during configuration change
    alertDialog = (AlertDialog) getLastCustomNonConfigurationInstance();

    if (alertDialog == null) {
        // Dialog is not retained, create and show it
        createAndShowDialog();
    } else {
        // Dialog was retained, show it again
        alertDialog.show();
    }
}





@Override
public Object onRetainCustomNonConfigurationInstance() {
    return alertDialog;
}


private void createAndShowDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Your Title")
           .setMessage("Your Message")
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Handle positive button click
               }
           });

    alertDialog = builder.create();
    alertDialog.show();
}
Leave a Comment