I am trying to replace a fragment in Android and its taking almost 2-3 seconds to load the fragment into the UI. I am not sure what might be causing the issue because I don't do perform heavy threads in the onCreateView
.
My fragment that is taking too much time to load has the following in its onCreateView
.
Link : Code for my onCreateView
All I do in my onCreateView is play around with the visibility of certain layouts depending upon the data from DB. I don't think that it would take me so much time to load the page, it doesn't make sense to me.
What have I tried so far :
I haven't tried anything solid except the following which are some silly implementations of stupidity or I think so..
Tried to place a major chunk of code in an Async Task but as most of the code requires setVisibility()
, it doesn't work in doInBackground()
. And adding a runOnUIThread()
is again same as not using a Async Task.
I doubted that the fragment manager might be taking some time to load the fragment into UI but when I replaced with another fragment with lesser UI components then it seemed to load fast.I also tried using getFragmentManager().executePendingTransactions();
.
So thats what I have tried so far but also found out that option 2 is completely unnecessary because the log that I print before the return convertView;
is taking 1-2 seconds to print. I am not sure what is going wrong or where its going wrong. Any help will be much appreciated.
You can use Async Task to perform the time-consuming tasks and take them off the UI thread. This will allow the fragment to load quickly and the list to populate "lazily". Another example here. The lag I described seems to be related to displaying data and not generating view.
Each tab will have a fragment to hold the products. The tabs could either held in activity or a fragment. I do find fragments a slightly faster than activities but really its not something you would really notice in most cases. Regardless if they was intended for speed or not they still seem/feel little quicker.
Like activities, they have a specific lifecycle, unlike activities, they are not top-level application components. Advantages of fragments include code reuse and modularity (e.g., using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets).
You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist.
You don't have to call super.onCreate()
in onCreateView
. Secondly, you are doing too much DB operations in oncreateView
by instructions like dbHelper.getRelationGenderForEdit
etc. You have to perform these operations in the backGroundThread
. You can use Loaders
or AsyncTasks
to achieve this and pass these results to Fragment
and use those values in onCreateView
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With