Untitled

 avatar
unknown
plain_text
a year ago
1.9 kB
1
Indexable
public class RetainedData {
    private AlertDialog alertDialog;

    public void setAlertDialog(AlertDialog alertDialog) {
        this.alertDialog = alertDialog;
    }

    public AlertDialog getAlertDialog() {
        return alertDialog;
    }
}



public class YourActivity extends AppCompatActivity {

    private RetainedData retainedData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        retainedData = (RetainedData) getLastCustomNonConfigurationInstance();

        if (retainedData == null) {
            // Retained data is not available, create and show the dialog
            createAndShowDialog();
        } else {
            // Retained data is available, show the dialog again
            showRetainedDialog();
        }
    }

    @Override
    public Object onRetainCustomNonConfigurationInstance() {
        // Save the data you want to retain during configuration change
        return retainedData;
    }

    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 alertDialog = builder.create();

        // Save the dialog to the retained data
        retainedData = new RetainedData();
        retainedData.setAlertDialog(alertDialog);

        alertDialog.show();
    }

    private void showRetainedDialog() {
        // Show the dialog again
        if (retainedData != null && retainedData.getAlertDialog() != null) {
            retainedData.getAlertDialog().show();
        }
    }
}
Editor is loading...
Leave a Comment