3

I've an android app which has TabLayout like this :

enter image description here

Each tab loads a Fragment and there 6 such tabs.

It has also navigation drawer like this :

enter image description here

on click of the options a new Activity gets open.

There are also few buttons on the Toolbar and on click of which new Activity gets open.

The app has also the Sliding up panel like this :

enter image description here

The problem is that when I open the app then after few seconds it shows me :

enter image description here

when this comes I can see in the logcat that

The application may be doing too much work on its main thread

The solution for it is given here

(this message occurs only on some of the devices) when we click on "OK" then the app continues to run but again after few seconds the same message pops up. I've noticed one more thing that when I leave the app open for longer period of time then this message doesn't come and app runs smoothly.

I want to know does the other Activities also gets loaded to run on main UI thread when we open the android app?

I think that the Fragments used in the ViewPager will get loaded on main UI thread when we open the app.

The code to add Fragments to ViewPager is like this :

tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        viewPagerAdapter.addFragments(new HomeFragment(), "Home");
        viewPagerAdapter.addFragments(new TrendingFragment(), "Best Deals");
        viewPagerAdapter.addFragments(new HomeServicesFragment(), "Home Services");
        viewPagerAdapter.addFragments(new EntertainmentFragment(), "Entertainment"); 
        viewPagerAdapter.addFragments(new FashionFragment(), "Fashion"); 
        viewPagerAdapter.addFragments(new VehicleServicesFragment(), "Vehicle Services"); 
        viewPagerAdapter.addFragments(new OthersFragment(), "Other Services"); 

        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

Also I want to know if I can use AsyncTask to load this TabLayout? Again I don't know which line are responsible for UI updation in above code. I'm not sure if doing this will help resolving the issue or not?

Community
  • 1
  • 1
Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
  • Try setting `viewPager.setOffscreenPageLimit(x)` so it doesnt always recreate `Fragments` – hrskrs Sep 07 '16 at 06:26
  • 1
    The issue here is inside your fragment. There must be something blocking UI thread. Are you making an api call inside those fragments? Or is there any read/write using database? If there any, you need to manage it using another thread (eg:background thread). – Amad Yus Sep 07 '16 at 06:29
  • (1) Declarations in main Application class (class which Extends Application), example database initiations (2) Content Providers initiation if any (3) Network operations should be either in AsyncTask/Service/Thread (4) Make sure Fragment's are having network logic in background operation and not in onResume – Sreehari Sep 07 '16 at 06:32
  • @AmadYus : I made my fragment empty and still the problem remains same. Yes there is read/write to database to my MainActivity.java but I'm using firebase for the database operations so I think that firebase manages its task asynchronously. – Amit Upadhyay Sep 07 '16 at 07:54

1 Answers1

0

My problem of getting the message

The application may be doing too much work on its main thread

is solved, and now I'm thinking to write it here. The issue was there in the drawables images, I was having an image with less size but high resolution and was using that in the ViewPager. On changing the images the problem went away.

Okay, above I've asked one more question that is :

I want to know does the other Activities also gets loaded to run on main UI thread when we open the android app?

This question may sound little wired because as we know that each Activity has its own life-cycle and it gets created and destroyed according to that. So of course only the Main Activity with intent-filter having category as LAUNCHER gets loaded when the application gets loaded. I asked this question because I was creating many SharedPreferences files on main UI thread in other Activities. So I thought creation of SharedPreferences file may be one reason for the problem.

Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57