Please tell me how when turning the Activity to save data in the AlertDialog.Builder dialog

There is an Activity in it code

AlertDialog.Builder alert = new AlertDialog.Builder(this); LayoutInflater layoutInflater = getLayoutInflater(); final View v1 = layoutInflater.inflate(R.layout.dialog_edit_car, null); alert.setView(v1); AlertDialog alertDialog = alert.show(); 

On the R.layout.dialog_edit_car layer are the EditText, RadioButton. Question: how to save their data when the activity is rotated. AlertDialog.Builder did not find the onSaveInstanceState and onRestoreInstanceState methods on AlertDialog.Builder. What is the right thing to do?

  • one
    Try to remake in DialogFragment - there all the necessary functionality is there - YuriySPb

1 answer 1

Did so

 public class MainActivitySettings extends AppCompatActivity { View viewSettingsDialog = null; //наш алерт диалог @Override protected void onCreate(Bundle savedInstanceState) {...} @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if(viewSettingsDialog!=null) { ((EditText) viewSettingsDialog.findViewById(R.id.editText_driveMarka)).setText(savedInstanceState.get("key").toString()); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if(viewSettingsDialog!=null) { outState.putString("key", ((EditText) viewSettingsDialog.findViewById(R.id.editText_driveMarka)).getText().toString()); } } protected void addCar(){ AlertDialog.Builder alert = new AlertDialog.Builder(this); LayoutInflater layoutInflater = getLayoutInflater(); viewSettingsDialog = layoutInflater.inflate(R.layout.dialog_edit_car, null); alert.setView(viewSettingsDialog); final AlertDialog alertDialog = alert.show(); ((Button) viewSettingsDialog.findViewById(R.id.buttonSaveSettings)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { alertDialog.cancel(); } }); } }