Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading a layout using ASyncTask in Android
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload