9

I have use the v4 support lib for FragmentTabHost

The requirement is that when I am switching tab one to another & another one, that is calling

onCreateView() & onActivityCreated() every time.

That's why my code performance is slow.

So, any other solutions? how to increase performance in fragment tab?

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Saunik Singh
  • 996
  • 1
  • 9
  • 19

4 Answers4

9

Sounds like a design smell.

Redesign your code so that heavy work is done asynchronously. Fragments should be able to be built quickly. If there is any large processing that needs to be done to in order for a Fragment to display useful information, that work should be done beforehand or asynchronously after the Fragment is created and the Fragment should be notified to update its content when the work is complete.

dcow
  • 7,765
  • 3
  • 45
  • 65
  • 3
    Agreed. If `onCreateView()` or `onActivityCreated()` take more than a couple of milliseconds combined, you're doing it wrong. – CommonsWare Apr 21 '13 at 13:40
1

First thing which you should take care of is to watch about calculations / loading a big set of data should be places on a different worker thread than main UI thread. The best option to do that (in my opinion) is to use AsyncTask. You can use something like this in your Fragment :

private class LoadData extends AsyncTask<Void, Void, Void>{

      @Override
      protected void onPreExecute(){
          super.onPreExecute();
          // this is the place where you can show
          // progressbar for example to indicate the user
          // that there is something which is happening/loading in the background
      }

      @Override
      protected void doInBackground(Void... params){

          // that's the place where you should do 
          // 'the heavy' process which should run on background thread
      }

      @Override
      protected void onPostExecute(Void result){
          super.onPostExecute();
          // you should update your UI here.
          // For example set your listview's adapter
          // changes button states, set text to textview and etc.
      }
}

This is the way you can make your tabs work faster.Hope this will help you! : )

hardartcore
  • 16,886
  • 12
  • 75
  • 101
1

I found a solution for that. I inserted all websevices & database transaction code in on create. because oncreate in not calling every time untill the ondestroy not call. & the other one solution is also available we can use

fragment.show();

& fragment.hide(); method

Saunik Singh
  • 996
  • 1
  • 9
  • 19
  • 1
    Sounds like you have the right idea. However, the ultimate goal is that Fragments shouldn't have to talk to their activity to retrieve data. They should be given all the resource references necessary fetch their data by themselves. After all that is the point of Fragments. Activities are merely containers responsible for initialization and navigation. – dcow Apr 26 '13 at 20:09
  • @saunik Sing: Can you pls tell where I have to use fragment.show() and fragment.hide(). Do I have to override onTabChanged Function ?? – Alok Rao Jul 24 '14 at 13:04
0

As an addition to Android-Developer: if you already are using AsyncTask, remember that even when you use multiple AsyncTask's, they are executed in the background, but all sequentially! If you want more threads to handle your tasks, check out this post, which perfectly explains how to achieve that! Running multiple AsyncTasks at the same time -- not possible?

Community
  • 1
  • 1
Toverbal
  • 256
  • 3
  • 14
  • 1
    This depends on which version you're running on and the device hardware. Async tasks are executed on a pool of threads in which the simple case is a pool of 1 worker thread. This is also the default for some versions of the framework. – dcow Dec 02 '13 at 20:57