title: Updating the text on a button
body: <p>Hi I was wondering why updating the text of a button behave different in my app.</p>
  
  <p>I call a fragment that gets the information what the button should be updated with, I do a interface-callback from the fragment to update a global variable that the activity updates the button text.</p>
  
  <p>The problem is that the activity where the button is located doesn't refresh the button to show the new text but if I do it through the fragment it works, invalidating the button doesn't work or force refresh if I do it through the activity.</p>
  
  <p>Here is the activity that calls when you press the button who then calls a fragment who shows a list and when you click on a option the button should change its text to whatever you have choosed:</p>
  
  <pre><code>public void onClick_testSite(View view)
  {
      // create the fragment
      SelectChartTypeDialogFragment chartType = new SelectChartTypeDialogFragment();
      // register you as a delegate for the callback
      chartType.delegate = this;
      // show the list
      chartType.show(getFragmentManager(), "WEE");
  
      // fetch the button and set the text ** DOESNT WORK **
      Button p1_button = (Button)findViewById(R.id.btn_pickChart);
      p1_button.setText(response);
      p1_button.invalidate();
  
  
  }
  
  @Override
  public void processFinish(String response)
  {
      this.response = response;
  }
  </code></pre>
  
  <p>and here is part of the fragment that handles the dialog:</p>
  
  <pre><code>@Override
  public Dialog onCreateDialog(Bundle savedInstanceState)
  {
      // get the list from a enum
      final List&lt;ChartTypes&gt; chartList = Arrays.asList(ChartTypes.values());
      // The array containing the different choices
      ArrayAdapter&lt;ChartTypes&gt; adapter = new ArrayAdapter&lt;ChartTypes&gt;(
              getActivity(), android.R.layout.simple_list_item_1, chartList);
      // Use the Builder class for convenient dialog construction
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  
      // The dialog setting
      builder.setTitle(R.string.pick_chart);
      builder.setAdapter(adapter, new DialogInterface.OnClickListener()
      {
          public void onClick(DialogInterface dialog, int position)
          {
              // get the name of the enum based on the position, what one
              // clicked on the dialog
              String strName = chartList.get(position).name();
  
              // this sets the text in the activitys global variable
              delegate.processFinish(strName);
              // set the text in the fragment instead
              //changeBtnText(strName);
  
              //dialog.dismiss();
          }
      });
  
      // Create the AlertDialog object and return it
      return builder.create();
  }
  
  public void changeBtnText(String newBtnxt)
  {
      Button button = (Button)getActivity().findViewById(R.id.btn_pickChart);
      button.setText(newBtnxt);
  }
  </code></pre>
  
  <p>My question is, why does it update the text on the gui(when running the app) through the fragment but not through the activity, i.e the method p1_button.setText(response);?</p>
  
  <p>EDIT answer as explained by Raghunandan:
  Problem was that I didn't understand that <code>onClick_testSite(View view)</code> completed even if you did not click anything on the dialog, I thought it waited with the function call of <code>chartType.show()</code> for it to return and then proceed to the end of the function.</p>