Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragmenttabhost performance is slow?

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?

like image 761
Saunik Singh Avatar asked Mar 19 '13 13:03

Saunik Singh


3 Answers

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.

like image 151
dcow Avatar answered Nov 12 '22 20:11

dcow


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! : )

like image 28
hardartcore Avatar answered Nov 12 '22 22:11

hardartcore


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

like image 1
Saunik Singh Avatar answered Nov 12 '22 20:11

Saunik Singh