title: Input dispatching timed out ANR
body: <p>I am getting the following ANR a lot in my application </p>
  
  <pre><code>Input dispatching timed out (Waiting to send key event because the focused window has not finished processing all of the input events that were previously delivered to it. Outbound queue length: 0. Wait queue length: 1.)
  </code></pre>
  
  <p>the log says that the problem in <strong>onActivityStopped</strong> method</p>
  
  <p>this is the code that I have added in this method</p>
  
  <pre><code>try {
              boolean foreground = new ForegroundCheckTask().execute(getApplicationContext()).get();
              if(!foreground) {
  
                  AdServerManager.getInstance().incrementImpression(activity, ForSaleDataManager.getInstance().getBannerImpression(),
                          ForSaleDataManager.getInstance().getOfferImpression(), new View.OnClickListener() {
                              @Override
                              public void onClick(View v) {
  
                                  ForSaleDataManager.getInstance().clearOfferImpression();
                                  ForSaleDataManager.getInstance().clearBannerImpression();
                              }
                          });
  
                  String lastOffers = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedOffersIds());
                  //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_OFFER_IDS.getValue() , lastOffers);
                  ForSaleDataManager.getInstance().clearVisitedOffersIds();
  
                  String lastCategories = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedCategoriesIds());
                  //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_CATEGORY_IDS.getValue() , lastCategories);
                  ForSaleDataManager.getInstance().clearVisitedCategoriesIds();
  
                  String lastSearch = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedSearchTerms());
                  //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_SEARCH_TERMS.getValue() , lastSearch);
                  ForSaleDataManager.getInstance().clearVisitedSearchTerms();
  
                  // clear landing page
                  PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.IS_CATEGORIES_LANDING_SHOWEN, false);
                  PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.IS_LISTING_LANDING_SHOWEN, false);
                  PhoneUtils.deleteWithPrefixFromSharedPreference(activity, ForSaleConstants.IS_SUB_CATEGORIES_LANDING_SHOWEN_PREFIX, false);
                  PhoneUtils.deleteWithPrefixFromSharedPreference(activity, ForSaleConstants.IS_SUB_CATEGORIES_LANDING_SHOWEN, false);
  
  
                  // reset for last activity
                  PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.SET_LAST_ACTIVITY_CALLED, false);
              }
          } catch (InterruptedException e) {
              e.printStackTrace();
          } catch (ExecutionException e) {
              e.printStackTrace();
          }
  </code></pre>
  
  <p>the system requirement of my app that I have to send some data to server and also clear local data so I have implemented the interface <strong>Application.ActivityLifecycleCallbacks</strong> </p>
  
  <p>can anyone advice how to meet my system requirement without generating ANR ?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>this is the code after I put it in AsyncTask and it does not work </p>
  
  <pre><code>@Override
      public void onActivityStopped(final Activity activity) {
  
          //new AsyncTask&lt;Void, Void, Void&gt;() {
  
              //@Override
              //protected Void doInBackground(Void... voids) {
  
                  try {
                      boolean foreground = new ForegroundCheckTask().execute(getApplicationContext()).get();
                      if(!foreground) {
  
                          AdServerManager.getInstance().incrementImpression(activity, ForSaleDataManager.getInstance().getBannerImpression(),
                                  ForSaleDataManager.getInstance().getOfferImpression(), new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
  
                                          ForSaleDataManager.getInstance().clearOfferImpression();
                                          ForSaleDataManager.getInstance().clearBannerImpression();
                                      }
                                  });
  
                          String lastOffers = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedOffersIds());
                          //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_OFFER_IDS.getValue() , lastOffers);
                          ForSaleDataManager.getInstance().clearVisitedOffersIds();
  
                          String lastCategories = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedCategoriesIds());
                          //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_CATEGORY_IDS.getValue() , lastCategories);
                          ForSaleDataManager.getInstance().clearVisitedCategoriesIds();
  
                          String lastSearch = TextUtils.join("," , ForSaleDataManager.getInstance().getVisitedSearchTerms());
                          //Appboy.getInstance(activity).getCurrentUser().setCustomUserAttribute(AppBoyAttributeName.LAST_TEN_SEARCH_TERMS.getValue() , lastSearch);
                          ForSaleDataManager.getInstance().clearVisitedSearchTerms();
  
                          // clear landing page
                          PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.IS_CATEGORIES_LANDING_SHOWEN, false);
                          PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.IS_LISTING_LANDING_SHOWEN, false);
                          PhoneUtils.deleteWithPrefixFromSharedPreference(activity, ForSaleConstants.IS_SUB_CATEGORIES_LANDING_SHOWEN_PREFIX, false);
                          PhoneUtils.deleteWithPrefixFromSharedPreference(activity, ForSaleConstants.IS_SUB_CATEGORIES_LANDING_SHOWEN, false);
  
  
                          // reset for last activity
                          PhoneUtils.addBooleanToSharedPreference(activity, ForSaleConstants.SET_LAST_ACTIVITY_CALLED, false);
                      }
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  } catch (ExecutionException e) {
                      e.printStackTrace();
                  }
  
                  //return null;
          //    }
          //}.execute();
  
  
      }
  </code></pre>