5

I am getting the following ANR a lot in my application

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.)

the log says that the problem in onActivityStopped method

this is the code that I have added in this method

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();
        }

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 Application.ActivityLifecycleCallbacks

can anyone advice how to meet my system requirement without generating ANR ?

EDIT

this is the code after I put it in AsyncTask and it does not work

@Override
    public void onActivityStopped(final Activity activity) {

        //new AsyncTask<Void, Void, Void>() {

            //@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();


    }
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

1 Answers1

1

Move all of that logic into a background thread. That is how you solve any ANR that comes from your code.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • it does not work I move the previous code in AsyncTask, it does not work and when I move app to background and bring it back to foreground , it close and reopen again, and please check my edit to view my code – Amira Elsayed Ismail Nov 14 '17 at 09:36
  • @AmiraElsayedIsmail: Your second code block seems to be the same as the first code block, except for more indentation and some comments. Beyond that, I have no idea what "it does not work" means, and I have no idea what "it close and reopen again" means. – CommonsWare Nov 14 '17 at 11:28
  • I used the second code block but for sure with out commented part, and the code that does not work is the code inside doinBackground(), and the last point that I put app in background while I am on HomeActivity and then when I bring it back to foreground the app open SplashActivity and start again – Amira Elsayed Ismail Nov 14 '17 at 12:20
  • @AmiraElsayedIsmail: "the code that does not work is the code inside doinBackground()" -- that does not explain what "does not work" means. Bear in mind that you should not be referencing the `Activity` from the background thread, as it might be destroyed before your thread completes. Instead, collect the data that you need from the activity first, then fork the thread and work off of that collected data. – CommonsWare Nov 14 '17 at 12:24