I have an android app with a MainActivity which implements a menu with 5 buttons. Each button activates a different fragment.
Without using the "setOffscreenPageLimit(int limit)", every time I go from fragment to fragment, each fragment loads every single time.
Using:
setOffscreenPageLimit(5)
I understand that loads all of my 5 main fragments when starting my application. But I don't want this, because it is too heavy.
I want to implement this:
"Load every fragment only when the user activates it, through choosing the menu button.Then keep it on memory , so when user goes back to this fragment, you don't have to load it again."
From the scratchy details you have provided, I understand that, you want to show fragment when it's displayed on the screen, you don't want it to load when fragment is in background. For this you can try this:
https://stackoverflow.com/a/25001920/6383029
Related
I am making an application that has MainActivity that contains BottomNavigationView and FrameLayout above it. There are 3 Fragments say Fragment A, B, and C.
My doubt is, How do I make switching of Fragments as quick as YouTube Android application? By saying quick, I mean that, when I am on "Home" tab of Youtube application and I switch to the "Trending" tab and again go back to the "Home" tab, it simply loads "Home" tab within fraction of seconds, as if it just hided the inflated page in background and showed up when selected from BottomNavigationView. And also, It inflates the page exactly to the same position where I left.
When I am trying to implement the same in my Application, the RecyclerView in Fragment A re-inflates if I come back from Fragment B.
I am expecting the idea how they do it and in which method they do it (For eg. onStart or onDestroy or onViewCreated)...
If you are using viewpager then Increase the viewpager offset limit
viewpager.OffscreenPageLimit = 2;
Tt's limit is one by default. I hope this may fix your issue.
Thanks
I am new to android development so forgive if i asked a silly question. Below i am narrating my requirement and the solution I thought of please read and help me if you have any solution.
In my application i have a activity that contain a toolbar on top, different options in the bottom and in the center area a framelayout where i will load fragments that will render different advertisements(Mostly images) based on the options the fragments using viewpager shows multiple images. Check the following image.
Now my problem is let's assume i selected Fashion option with featured sub option then title should be changed to Fashion-Featured and Fragment should show images related to Fashion>featured let's call this fragment fragment1. Now if the user tap on an image then the application will render another fragment lets call it fragment2.
In the fragment2 the selected image will be rendered in large with two buttons More and Stores. Here if the user taps on more button then the thumbnail images of fashion>featured option will be displayed so that user can select another image. If the user taps on store button then the another fragment will load let's call it fragment3.
In fragment3 the stores selling that particular product will be listed.
Now my problem is how can i pass the data between activity to fragment1 then fragment1 to fragment2 then from fragment2 to fragment3 so that the if the user press backbutton still the fragment2 should show the fashion>featured images with the tapped image on large and if the user press another backbutton then fragment1 should show fashion>featured option images. The data passing should be fail safe that is if the app goes in the background then come to foreground then also it should work.
Please help me out. I am planning to implement using sqlite database and reference in shared preference. I think this is not an exceptional requirement other application also handles this kind of requirement so i need a better way to achieve this.
If you would like to pass some data to a new fragment see this answer: How to pass a variable from Activity to Fragment, and pass it back?
If your fragment is already created you can add some method to it and call this method with required argument.
I have a fragment initially inside my activity, this fragment contains a button that's when it's clicked it open the second fragment, and the second fragment contains a button that's when it's clicked it open the third fragment, and so on ...
Now I want to test the launch of the third fragment after passing by the 2 old fragment (not testing the fragment in isolation), I tried to preform a click in the button inside each fragment but it seems espresso doesn't wait for the 2 fragment to launch, before clicking the button inside it, is there any solution for this ?
Without seeing the code its hard to tell what the best way to handle this would be.
However this seems like an issue with the IdlingResource. Espresso should wait until 2nd fragment has finished launching before moving forward.
You can handle it by adding a SystemClock.sleep(int milliseconds) . That is not an ideal solution however and sleeps should be avoided at all costs.
No it's not a timing issue I have an Activity with 3 fragments all of which are visible concurrently. Android Espresso will only do tests in the top fragment and refuses to acknowledge the presence of all the rest of the fragments.
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 building an app that consists of actionbar with tabs, and each tab consists of a fragment. One of the fragments has data that need to be loaded. So I want the data to be loaded only the first time. When the user changes the tabs and goes back to the one containing that fragment, doesn't wait for the data to be loaded again. I assume I have to use some kind of saving the data so that it can be used the next time the user goes back to that particular fragment. But I just do not know what method I should use. If someone knows or can suggest an example of saving fragment state, please let me know. Thank u!
Try this:
myFragment.setRetainInstance(true);
It'll mean the fragment isn't recreated every time it's added.