Untitled
unknown
plain_text
2 years ago
1.1 kB
9
Indexable
@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();
}Editor is loading...
Leave a Comment