title: Async Task Implementation In Xamarin Android
body: <p>Async Task has seemed to be one of the toughest implementation recently. I actually do not want to use Multithreading here but async tasks. In my code below, i have implemented async task to fetch my data from firebase to my app. Even though i am using async task, this is what i am experiencing on the user interface. </p>
  
  <p>I have 3 activities. The app launches into the 1st activity without the user experiencing any form of lagging. When i navigate from the 1st activity to the 2nd activity, the navigation drawer layout lags for some seconds before data appears for 2nd activity and so for activity 3. There after, it moves a bit smoothly when i navigate between the 3 activities. (Meaning it lags just once moving from one activity to another before it moves smoothly). </p>
  
  <p><strong>please note that when i comment out <code>this.refreshAll()</code> in the OnResume(), it moves smoothly</strong></p>
  
  <p>Is my async well implemented? </p>
  
  <p><strong>Activity2</strong></p>
  
  <pre><code>protected override void OnCreate(Bundle savedInstanceState)
  {
      base.OnCreate(savedInstanceState);
      fab = FindViewById&lt;FloatingActionButton&gt;(Resource.Id.fab);
  
      fab.Click += delegate
      {
          LayoutInflater HeroInflater = LayoutInflater.From(this);
          View mView = peaceHeroInflater.Inflate(Resource.Layout.HeroDialog, null);
          Android.Support.V7.App.AlertDialog.Builder alertDialogBuilder = new Android.Support.V7.App.AlertDialog.Builder(this);
          alertDialogBuilder.SetView(mView);
  
          var HeroName = mView.FindViewById&lt;EditText&gt;(Resource.Id.editText1);
          alertDialogBuilder.SetCancelable(false)
              .SetPositiveButton("Save", async delegate
              {
                  Hero hero = new Hero();
                  hero.Uid = string.Empty;
                  hero.Name = HeroName.Text;    
                  var firebase = new FirebaseClient(FirebaseUrl);                         
                  var item = await firebase.Child("sometable").PostAsync&lt;PeaceHero&gt;(hero);
  
                  await ReloadData();   
              })  
              .SetNegativeButton("Cancel", delegate
              {
                  alertDialogBuilder.Dispose();
              });
  
          Android.Support.V7.App.AlertDialog alertDialogAndroid = alertDialogBuilder.Create();
          alertDialogAndroid.Show();
      };
  }
  
  private async void refreshAll()
  {
      await ReloadData();
  }
  
  private async Task ReloadData()
  {
      var firebase = new FirebaseClient(FirebaseUrl);
      var items = await firebase.Child("sometable").OnceAsync&lt;&gt;();
  
      foreach (var item in items)
      {
          Hero details = new Hero();
          details.Uid = item.Key;
          details.Name = item.Object.Name;
          add_Hero.Add(details);
      }
  
      rv.SetLayoutManager(new LinearLayoutManager(this));
      rv.SetItemAnimator(new DefaultItemAnimator());                    
  }
  
  protected  override void OnResume()
  { 
      base.OnResume();
      this.refreshAll();
  }
  </code></pre>
  
  <p><strong>update</strong></p>
  
  <pre><code>private async  Task refreshAll()
          {
  
              await RefreshData();
  
  
          }
  
  
   protected async override void OnResume()
          { 
              base.OnResume();
              await refreshAll();
  
          }
  </code></pre>
  
  <p><strong>LOG</strong></p>
  
  <pre><code>[Mono] AOT: image 'Mono.Security.dll.so' not found: dlopen failed: library "/data/app-lib/com.companyname.peaceapp.peaceapp-130/libaot-Mono.Security.dll.so" not found
  [Mono] AOT: image '/usr/local/lib/mono/aot-cache/arm/Mono.Security.dll.so' not found: dlopen failed: library "/data/app-lib/com.companyname.peaceapp.peaceapp-130/libaot-Mono.Security.dll.so" not found
  [Mono] Config attempting to parse: 'Mono.Security.dll.config'.
  [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/Mono.Security/Mono.Security.config'.
  [Mono] Assembly Ref addref System[0x86e311a8] -&gt; Mono.Security[0x8792d7e8]: 2
  [Mono] Assembly Ref addref Mono.Security[0x8792d7e8] -&gt; mscorlib[0x73d41ac0]: 68
  Loaded assembly: Mono.Security.dll [External]
  [Mono] Assembly Ref addref System.Net.Http[0x8649e068] -&gt; System.Core[0x7f2bf708]: 9
  [Mono] Assembly Ref addref Mono.Android[0x7f1692f0] -&gt; System[0x86e311a8]: 7
  Thread started: &lt;Thread Pool&gt; #4
  Thread started: &lt;Thread Pool&gt; #5
  [Mono] [0x87aecc90] worker starting
  Thread started: &lt;Thread Pool&gt; #6
  [Mono] [0x87b045b0] worker starting
  Thread started: &lt;Thread Pool&gt; #7
  [Mono] [0x87ae1a88] worker starting
  [Choreographer] Skipped 80 frames!  The application may be doing too much work on its main thread.
  [RecyclerView] No adapter attached; skipping layout
  [ProgressBar] updateDrawableBounds: left = 72
  [ProgressBar] updateDrawableBounds: top = 0
  [ProgressBar] updateDrawableBounds: right = 72
  [ProgressBar] updateDrawableBounds: bottom = 0
  [Mono] [0x87aecc90] hill climbing, change max number of threads 4
  [RecyclerView] No adapter attached; skipping layout
  [Mono] Assembly Ref addref Mono.Security[0x8792d7e8] -&gt; System[0x86e311a8]: 8
  Thread started: &lt;Thread Pool&gt; #8
  Thread started: &lt;Thread Pool&gt; #9
  [Mono] [0x851329e8] worker starting
  Thread started: &lt;Thread Pool&gt; #10
  [Mono] [0x85132de0] worker starting
  [Mono] Unloading image System.dll [0x851046d8].
  [Mono] Image addref System[0x8548b280] -&gt; System.dll[0x86e30720]: 5
  [Mono] Config attempting to parse: 'System.dll.config'.
  [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System/System.config'.
  [Mono] Unloading image System.dll [0x851046d8].
  [Mono] Image addref System[0x8548f438] -&gt; System.dll[0x86e30720]: 6
  [Mono] Config attempting to parse: 'System.dll.config'.
  [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System/System.config'.
  [Mono] Unloading image System.dll [0x851046d8].
  [Mono] Image addref System[0x854829d8] -&gt; System.dll[0x86e30720]: 7
  [Mono] Config attempting to parse: 'System.dll.config'.
  [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System/System.config'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_is_assignable_from'.
  [Mono] Probing 'java_interop_jnienv_is_assignable_from'.
  [Mono] Found as 'java_interop_jnienv_is_assignable_from'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_get_byte_array_region'.
  [Mono] Probing 'java_interop_jnienv_get_byte_array_region'.
  [Mono] Found as 'java_interop_jnienv_get_byte_array_region'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_set_byte_array_region'.
  [Mono] Probing 'java_interop_jnienv_set_byte_array_region'.
  [Mono] Found as 'java_interop_jnienv_set_byte_array_region'.
  [Mono] The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0
  [Mono] Unloading image System.Core.dll [0x854f61d0].
  [Mono] Image addref System.Core[0x85501bf0] -&gt; System.Core.dll[0x7f2bedb0]: 5
  [Mono] Config attempting to parse: 'System.Core.dll.config'.
  [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Core/System.Core.config'.
  [Mono] The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0
  [Mono] Unloading image System.Core.dll [0x855214c8].
  [Mono] Image addref System.Core[0x84ff8888] -&gt; System.Core.dll[0x7f2bedb0]: 6
  [Mono] Config attempting to parse: 'System.Core.dll.config'.
  [Mono] Config attempting to parse: '/usr/local/etc/mono/assemblies/System.Core/System.Core.config'.
  [Mono] [0x851329e8] hill climbing, change max number of threads 5
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_get_static_field_id'.
  [Mono] Probing 'java_interop_jnienv_get_static_field_id'.
  [Mono] Found as 'java_interop_jnienv_get_static_field_id'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_get_static_object_field'.
  [Mono] Probing 'java_interop_jnienv_get_static_object_field'.
  [Mono] Found as 'java_interop_jnienv_get_static_object_field'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_call_boolean_method_a'.
  [Mono] Probing 'java_interop_jnienv_call_boolean_method_a'.
  [Mono] Found as 'java_interop_jnienv_call_boolean_method_a'.
  Thread finished: &lt;Thread Pool&gt; #10
  [Mono] [0x85132de0] worker finishing
  </code></pre>