Custom Android TabHost Slow Slices

I use ActionbarSherlock and SlidingMenu.

My MainActivity does not access the database and does not analyze what ever. Everything is static.

Tabs are generated dynamically depending on the "Section" that you select from SlidingMenu. After 2 clicks, the application becomes significantly slow.

Below is my main look.

<TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" /> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#0000FF" > </FrameLayout> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" > </FrameLayout> </LinearLayout> </TabHost> </LinearLayout> 

Code used to manage Tabhost and fragments:

 public void selectPage(MenuPageItem page, Page subNavigationPage) { if (page == null || subNavigationPage == null || (page.equals(selectedPage) && subNavigationPage.equals(selectedSubNavigation))) { return; } Log.d(TAG, "Show the page: " + page.getTitle() + " / subNavigationPage: " + subNavigationPage.getTitle()); boolean needUpdateTabUI = !(page.equals(selectedPage)); selectedPage = page; selectedSubNavigation = subNavigationPage; // 1) Manage tabHost if (needUpdateTabUI) { Log.d(TAG, "need to update tab layout"); updatePageTabUI(); } // Change selected tab mTabHost.setCurrentTabByTag(selectedSubNavigation.getTitle()); // 2) Change Contentfragment Log.d(TAG, "update fragment"); // TODO: check the current fragment is not already the good one and just // call its updateData method updatePageFragment(); // Close the sliding menu if opened if (getSlidingMenu().isShown()) { Handler h = new Handler(); h.postDelayed(new Runnable() { public void run() { getSlidingMenu().showContent(); } }, 50); } } private void updatePageTabUI() { if (selectedPage.getPages().size() > 1) { // More than one sub navigation -> show the tab host // mTabHost.setVisibility(View.VISIBLE); mTabHost.setup(); mTabHost.clearAllTabs(); int i = 0; for (final Page subnav : selectedPage.getPages()) { Log.d(TAG, "add tab tag=" + subnav.getTitle() + " title=" + subnav.getTitle()); mTabHost.addTab(mTabHost.newTabSpec(subnav.getTitle()).setIndicator(subnav.getTitle()) .setContent(new EmptyTabFactory())); mTabHost.getTabWidget().getChildAt(i).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { selectPage(selectedPage, subnav); } }); i++; } } else { // Only one sub navigation -> hide the tab host // mTabHost.setVisibility(View.GONE); mTabHost.setup(); mTabHost.clearAllTabs(); } mTabHost.requestLayout(); mTabHost.invalidate(); } private void updatePageFragment() { try { PageFragment fragContent = (PageFragment) selectedSubNavigation.getFragmentClass().newInstance(); fragContent.setSubNavigationPage(selectedSubNavigation); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.content_frame, fragContent); fragmentTransaction.commit(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } 
+4
source share
2 answers

@ Marco's answer is not bad, I just accidentally found a "real" criminal. I will study the path that Marco pointed out.

I found that inside the SlidingMenu adapter we had a ViewBadger (lib) and it was not reused correctly, creating views every time the fragment changed.

 BadgeView b = new BadgeView(getActivity(), holder.icon); holder.badger = b; convertView.setTag(holder); 
+1
source

You save all your work on UIThread, and therefore you end up with a slow response (for example, this is one of the reasons why it is forbidden to create networks in the user interface thread on the latest Android OS). Try changing your concept a bit and create a tab in the background in different threads. Or at least lock the user interface and show a wait dialog when you do your work so that the user can have the proper interface.

Hope this helps and enjoy your work.

+1
source

Source: https://habr.com/ru/post/1480734/


All Articles