As per the answer here How to make fragments load faster? I put all tasks in an async task and still there's a delay while loading fragment. The fragment is a calculator, with 30 buttons and two text views.
I have a sliding drawer and the delay occurs when I choose the item that opens the calculator fragment. The sliding drawer lags while returning to hidden position.
I use fragmentTransaction.replace(R.id.content_frame, calc_frag) in a fragment activity to load the fragment.
I removed all the code in the fragment except inflating the layout. Lag.
How I get rid of the delay/lag?
Without code its hard to say, maybe you should look at ProgressBar and hold on showing the fragment until the contents have fully loaded?
Related
I am pretty new to Android app development so it's difficult for me to keep all the relevant concepts straight in my head, but I will explain what I want as easily as I can.
My MainActivity loads a TopActionBar fragment (circled in red) that I have created. Within the TopActionBar fragment, I load my HomeFragment fragment I created into a FrameLayount within my TopActionBar fragment. The buttons (circled in green) each load a new Activity. Each of these new Activities loads the TopActionBar fragment just with different parameters to load the correct title for the action bar depending on the tab. The issue is that the MaterialToolbar has the Activity transition animation applied to it (currently just the default transition). I want the rest of the content loaded when changing the Activity to have a transition animation except for the actual MaterialToolbar at the top of the screen. Is there a way for this to be done, or is there a different design approach I should be considering.
Here is an sample image of my UI:
My main fragment has too many views to load because the lines of code in the file are increasing. To avoid this I decide to separate views using a child fragment. So now upper views are in the child fragment and the remaining bottom views are in the main fragment. Till this ok.
Now I am opening a new fragment by clicking one view from the main fragment. When I came back to the main fragment it is reloading the child fragment because of that I am getting NullPointerException and the app crashed.
Following is the way I am adding child fragments.
childFragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commitAllowingStateLoss()
For more understanding.
I am using Navigation with BottomNavigationBar.
How to avoid this?
In some cases, fragment views are flickering when back to that fragment. How to avoid that?
For your first question you want to avoid putting the fragment in the backstack.
childFragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)//<-- Here
.commitAllowingStateLoss()
For your second question Fragments are destroyed to conserve memory,typically happens when you can't see it. As such the recreation process could take some time, depending on how heavy the view is, thus the blinking.
The only way to stop it from blinking is to make sure the Fragment isn't doing so much that it can't seamlessly load back in.
You could do this by lazy loading your more heavy views, like lists, videos, and large images.
You could also look into Fragment transitions. These could visually smooth out the process of loading.
Here is a great source for some standard animations.
Fragment transaction animation: slide in and slide out
I'm making an app that uses a bottom navigation bar (BottomBar) on its MainActivity. A FrameLayout is positioned above the BottomBar. Every time an icon in the bottom bar is clicked, the tab's fragment is inserted into the FrameLayout using FragmentTransaction's replace() function. That inserted fragment contains ViewPager, which results in more fragment launching and caching. Right now, there is only one Activity but many Fragments. I haven't load any data and I'm already receiving a OutOfMemory Exception even after setting the maxHeapSize to 4gb.
What can I do to improve the performance without changing the UI (still have a bottom navigation bar and each tab contains a viewpager)?
I have an app that initially displays one fragment, when a button is clicked it transitions to the next fragment. However inside the 2nd fragment there's a ViewPager that holds images from a URL. It can take some time to process all the images. And it is processing the images as the button is clicked which then slows down the speed the fragment gets set up, which takes a lot of time. I want the fragment to be there and prepared instantly.
I can fetch the images in the mainActivity. I however cannot set them to the ViewPager before the fragmentTransaction is committed because the ViewPager doesn't exist until the onCreateView method occurs in the fragment which is only called when the transaction is committed. Is there any way to have the fragment set up in the activity before I commit the transaction.
So the user can easily switch between the two fragments without having to wait for them to process. Or should I have rather used tabs. The thing is I don't want a tabs bar to appear at the top of the screen. I want the tabs to change when by button is clicked.
It sounds like you're processing the images in the main thread? I think it would be best if, instead, you just pass the URL to your second fragment and have it use a Loader to process those images in the background, with placeholders for each image as it's getting loaded.
If you insist on doing the loading in the first fragment, you should just be able to set up and add your second fragment without any images, but have it be hidden. Then, once your loading is complete, you will still have access to your ViewPager from your first fragment and can use another transaction to show the second fragment when ready.
P.S. it would be easier to read/understand your question if it was laid out more logically and not a huge paragraph.
I am trying to develop an app in which fragments are involved. There are 2 fragments on screen. The list fragment containing a Start and Stop button and a detail fragment on the right side. On click of the Start button an audio processing code runs in the detail fragment. So depending on the result i get out of the process I want to change the layout of the fragment. Basically I want to change the layout file of a single fragment during run time depending on the results I get while doing some process. How can I achieve it?
Thanks!!
You cannot switch to a different layout file in a fragment, the view you returned from onCreateView cannot be replaced.
However, you have several options:
You can show and hide views at runtime with View.setVisibility().
ViewStubs can be inflated at a later time.
Or you could replace your detail fragment with a new fragment.