title: Loading a layout using ASyncTask in Android
body: <p>I have an application that loads a dialog with some images specified in the dialog's XML layout. I've read a lot about using images in Android and being careful with the VM budget so I believe that any handling of images with the potential to take up a decent amount of memory should be handled off of the main thread. With that said I was wondering if it is wise to load an entire layout off of the UI thread using an ASyncTask. I have a working code but I couldn't find out if this was good practice through some Google searches. Here is the code below.</p>
  
  <p>This is the case statement that triggers when the user presses a button to load the dialog.</p>
  
  <pre><code>case R.id.showDialog:
  
      vibrator.vibrate(vibrateMilis);
  
      mDialog = new Dialog(getActivity(), R.style.CustomDialog);
  
      new LoadLayout().execute("");
  
  break;
  </code></pre>
  
  <p>And here is the ASyncTask</p>
  
  <pre><code>private class LoadLayout extends AsyncTask&lt;String, Void, String&gt; 
  {
  
      ProgressDialog progressDialog;
  
          @Override
          protected String doInBackground(String... params) 
          {
  
              mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
              mDialog.setContentView(R.layout.dialog_layout);
              mDialog.setCancelable(true);
  
              return null;
          }
  
          @Override
          protected void onPostExecute(String result)
          {
  
              progressDialog.cancel();
  
              mDialog.show();
  
          }
  
          @Override
          protected void onPreExecute() 
          {
  
              progressDialog = new ProgressDialog(
                      getActivity());
              progressDialog.setMessage("Loading...");
              progressDialog.setCancelable(false);
              progressDialog.show();
  
          }
  
          @Override
          protected void onProgressUpdate(Void... values) 
          {
  
              // Do nothing
  
          }
  
  }
  </code></pre>
  
  <p>So this code works but the question I have is this. Is this considered good practice? It seems a bit hacky for my taste. I didn't come across this with multiple Google searches so that's why I'm a bit concerned. I mean if it was good practice it would've been documented somewhere right?</p>
  
  <p>Thanks for any input.</p>