title: Android Retrofit2: How to read and set EditText values when fragment is created?
body: <p>I'm trying to read meteorological values like temperature, pressure... and I have some EditText in the main fragment layout, that opens immediatly when the application is opened.</p>
  
  <p>The point is that I wanna call using Retrofit2 my API developed with Slim PHP, to read these values and then set the EditText texts with this values.</p>
  
  <p>These are the onCreate and onCreateView methods I have in the fragment class:</p>
  
  <pre><code>@Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
      this.readSummaryData();
  }
  
  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.fragment_patient_index, container, false);
  
      editTextTemperature = v.findViewById(R.id.teperaturaTxt_patientMain);
      editTextPressure = v.findViewById(R.id.pressureTxt_patientMain);
      editTextHumidity = v.findViewById(R.id.humidityTxt_patientMain);
  
      editTextTemperature.setText(((temperature != null)) ? String.valueOf(temperature) : "NaN") + " ºC");
      editTextPressure.setText(((pressure != null) ? String.valueOf(pressure) : "NaN") + " hPa");
      editTextHumidity.setText(((humidity != null) ? String.valueOf(humidity) : "NaN") + " %");
  return v;
  }
  </code></pre>
  
  <p>And this is the readSummaryData method:</p>
  
  <pre><code>private void readSummaryData(){
      Call&lt;ReadSummaryDataResponse&gt; readSummaryData = RetrofitClient.getInstance().getAPI().readSummaryData();
      readSummaryData.enqueue(new Callback&lt;ReadSummaryDataResponse&gt;() {
          @Override
          public void onResponse(Call&lt;ReadSummaryDataResponse&gt; call, Response&lt;ReadSummaryDataResponse&gt; response) {
              try {
                  ReadSummaryDataResponse summaryDataResponse = response.body();
                  if(!summaryDataResponse.getError()){
                      temperature = Math.round(summaryDataResponse.getSummary().getTMP() * 100.0) / 100.0;
                      pressure = Math.round(summaryDataResponse.getSummary().getPRE() * 100.0) / 100.0;
                      humidity = Math.round(summaryDataResponse.getSummary().getHR() * 100.0) / 100.0;
                  }
                  else{
                      Toast.makeText(getActivity(), "ERROR", Toast.LENGTH_SHORT).show();
                  }
              }
              catch (Exception e){
                  Log.e("Exception", e.getMessage());
              }
          }
  
          @Override
          public void onFailure(Call&lt;ReadSummaryDataResponse&gt; call, Throwable t) {
  
          }
      });
  }
  </code></pre>
  
  <p>The summaryData response contains the summary class which has the temperature, pressure and humidity values.</p>
  
  <p>So when I open this fragment when starting the application the values ​​of the EditText are all "NaN", but if I go to another fragment, and then come back to the main fragment the values aren't "NaN".</p>
  
  <p>I think this may be because the EditText.setText(String) is done before the readSummaryData, and I don't know how to solve it.</p>
  
  <p>Thanks everyone</p>