title: How to stay in an activity if the Internet connection is lost in between an asynchorous task
body: <p>I have a button in an activity. When I press it, it will load another activity. In this activity I have a custom grid view with a description and an image in each cell. I am populating the grid view from a remote server through an asynchronous task:</p>
  
  <pre><code>button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
  
          if (isInternetAvailable) {
              Intent intent = new Intent(menuActivity.this,
                      gridActivity.class);
              startActivity(intent);
          }
          else {
              MyAlertDialog.ShowAlertDialog(menuActivity.this,
                                            "",
                                            "No network connection",
                                            "OK");
          }
      }
  });
  </code></pre>
  
  <p>I am sharing the testAsyncTask() here. I am calling this function in the onCreate() of  gridActivity (the second activity)</p>
  
  <pre><code>private void testAsyncTask() {
      new AsyncTask&lt;Object, Object, Object&gt;() {
  
          @Override
          protected void onPreExecute() {
  
              progress_Dialog = ProgressDialog.show(a, "", "Loading");
          }
  
          @Override
          protected Integer doInBackground(Object... params) {
              try {
                  MenuService menuService = new MenuServiceImpl();
                  MenuServiceResponse pMenu = menuService.getMenu();
                  itemlist = pMenu.getMenu().getMenus();
                  return 0;
              }
              catch (MyServiceException e) {
                  MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");
                  e.printStackTrace();
              }
              return 0;
          }
  
          @Override
          protected void onPostExecute(Object result) {
  
              if (progress_Dialog != null) {
                  progress_Dialog.dismiss();
              }
  
              try
              {
                  adapter = new GridAdapter(
                      GridActivity.this, itemlist);
                  AllitemgridView.setAdapter(adapter);
  
              }
              catch(Exception e)
              {
                  adapter = null;
                  MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "Check Network Connection", "OK");
              }
  
          }
      }.execute();
  }
  </code></pre>
  
  <p>I am checking a condition in the first activity where I kept the button. I will fire the start activty only if I have an Internet connection.</p>
  
  <p>Now in to my problem, if I have an Internet connection and I press the button, startacvitiy will fire, and it will move to the second activity. In the onCreate() of the second activity I'm calling the testAsync(). Now it will show a loading dialog box and in the background I will fetch the data from a remote server.</p>
  
  <p>Now if I switch my modem off, the application freezes for 5-6 seconds and then it will force close. So how can I handle this exception?</p>
  
  <p>The coding inside the doInbackgorund() requires an Internet connection and that could be the reason for this crash when I disconnect 
  from Wi-Fi. How can I handle this crash?</p>