質問

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?

役に立ちましたか?

解決 3

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

他のヒント

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.

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

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?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top