title: Show a loading spinner while a fragment is loading
body: <p>I have developed a fragment-based app. </p>
  
  <p>I have a menu fragment with buttons,these buttons open a new fragment each one, replacing the last one. </p>
  
  <p>The issue is, that some fragment takes a while in opening cause it does some calls to asynctasks and populates some listviews.</p>
  
  <p>So when I press the button in the menu fragment, it keeps freezed 2 seconds     until the new fragment appears replacing the menu fragment.</p>
  
  <p>I would like that a spinner or "loading.." dialog appears in that time.</p>
  
  <p>I have tested this</p>
  
  <pre><code>        private progressDialog progressDialog;
          progressDialog = new ProgressDialog(this);
          progressDialog.setIndeterminate(true);
          progressDialog.setMessage("Loading...");
          progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          progressDialog.show();
  </code></pre>
  
  <p>This code show the dialog but it never shows it when the screen is freezed.</p>
  
  <p>Where I should place that code? in the activity which contains all fragments or in the menu fragment? or maybe in the fragment which is loaded?</p>
  
  <p>I dont get to accomplish this so when in my menu fragment I press the button I do the following code.</p>
  
  <pre><code>         NewAppointmentFragment fragment = new NewAppointmentFragment();
                  android.support.v4.app.FragmentTransaction fragmentTransaction =
  
          getActivity().getSupportFragmentManager().beginTransaction();
                  fragmentTransaction.replace(R.id.fragment_container, fragment, 
          "NewAppointmentFragment");
                  fragmentTransaction.addToBackStack(null);
                  fragmentTransaction.commit();
  </code></pre>
  
  <p>but it takes 2 seconds until this new fragment is loaded and appears
  The  freezed 2 seconds you can see the menu fragment with the button pressed</p>
  
  <p><strong>Could be cause in the new fragment I call all the asynctasks and operations to populate listviews in  OnCreateView ?</strong></p>
  
  <p>How could I resolve this issue?</p>
  
  <p>Thanks in advance</p>
  
  <p>My menu fragment </p>
  
  <pre><code>      @Override
           public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
           View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  
  
           nextAppointmentsButton = (Button) 
           rootView.findViewById(R.id.nextAppointmentsButton);
           nuevaCitaButton = (Button) rootView.findViewById(R.id.nuevaCitaButton);
           nearbyPharmaciesButton = (Button) 
           rootView.findViewById(R.id.nearbyPharmaciesButton);
           ourLocationButton = (Button) rootView.findViewById(R.id.ourLocationButton);
  
           nextAppointmentsButton.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                  UpcomingAppointmentsFragment fragment = new 
           UpcomingAppointmentsFragment();
                  android.support.v4.app.FragmentTransaction fragmentTransaction =
  
            getActivity().getSupportFragmentManager().beginTransaction();
                  fragmentTransaction.replace(R.id.fragment_container, fragment);
                  fragmentTransaction.addToBackStack(null);
                  fragmentTransaction.commit();
  
              }
  
           });
  
           nuevaCitaButton.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   ((MainActivity)getActivity()).showProgressDialog();
                   NewAppointmentFragment fragment = new NewAppointmentFragment();
                   android.support.v4.app.FragmentTransaction fragmentTransaction =
  
           getActivity().getSupportFragmentManager().beginTransaction();
                  fragmentTransaction.replace(R.id.fragment_container, fragment, 
           "NewAppointmentFragment");
                  fragmentTransaction.addToBackStack(null);
                  fragmentTransaction.commit();
              }
  
          });
  
          nearbyPharmaciesButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  NearbyPharmaciesFragment fragment = new NearbyPharmaciesFragment();
                  android.support.v4.app.FragmentTransaction fragmentTransaction =
  
          getActivity().getSupportFragmentManager().beginTransaction();
                  fragmentTransaction.replace(R.id.fragment_container, fragment);
                  fragmentTransaction.addToBackStack(null);
                  fragmentTransaction.commit();
  
              }
  
          });
          ourLocationButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  OurLocationMapFragment fragment = new OurLocationMapFragment();
                  android.support.v4.app.FragmentTransaction fragmentTransaction =
  
          getActivity().getSupportFragmentManager().beginTransaction();
                  fragmentTransaction.replace(R.id.fragment_container, fragment);
                  fragmentTransaction.addToBackStack(null);
                  fragmentTransaction.commit();
              }
  
          });
  
  
          // Inflate the layout for this fragment
          return rootView;
          }
  </code></pre>
  
  <p>My new Fragment loaded when I press the menu button</p>
  
  <pre><code>      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                       Bundle savedInstanceState) {
         View rootView = inflater.inflate(R.layout.fragment_new_appointment, 
          container, false);
  
        // **Calls to asynctasks **
        // **Populate operations ListViews**
  
        return rootView;
        }
  </code></pre>