I have an application using fragments. One of these fragments a WebView which show some locally generated html content. It could have different size but not too large (1-30kb). When user clicks a button on one fragment (let it be FragmentA) i need to change it with that WebView fragment (FragmentB).
How is it going now:
1. Instaniate FragmentB
2. replace FragmentA with FragmentB (user sees blank white screen)
3. FragmentB loads generated html, render it (user sees top of html)
4. Page scrolls down with webView.pageDown(true) (calling it from opPageFinished()) (user sees scrolling animation to the bottom of the page)
How i want it to be:
1. Instaniate FragmentB
2. set it to some invisible container (user still have FragmentA on the screen)
3. it renders html, scrolls it down (same)
4. when page is ready, rendered and scrolled down I hide FragmentA and show FragmentB
Any suggestions how I can achieve that?
I tried the second algorithm I described but the problem that it looks like WebView dont want to render and\or scroll content untill the fragment is visible to user.
After the user pressed the button in Fragment A, then I would switch to Fragment B immediately.
Fragment B would contain your webview, and something that overlaps the webview to hide it. It could be anything, even a simple image, or a loading animation. In onPageFinished(), you should call to WebView.pageDown(true), and hide the overlapping layer to show the webview.
Okay, I figured it out myself. The answer is simple.
I'm placing FragmentA and FragmentB inside FrameLayout so that FragmentA is "on top" of FragmentB. So when I need to show B i'm just hide A. That's all. B is always "visible" and can render its content in background behind A.
Related
I have an frame in an activity which received fragments based on button clicks at the bottom menu of the activity.
The Fragment I I have in question is structured like below. If I click off of the fragment when i'm at the scrolltop to another and then back to this one it stays at the top but if I scroll 1/2 way down the fragment and then leave and come back it is like 10% scrolled from the top. an odd behavior i'm not sure how to rectify.
Main Activity ->>viewpager1(frameLayout)->fragment1->frag1_constraint(ScrollView)
Fragment 1 Layout
Main Activity Layout
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 have in my app a drawer navigation, which contain few fragments to go to. when my app starts it opens by default the first fragment - which contains a view pager that has 2 fragments in it as well. in those 2 fragments I have a text view and a button as well.
the problem is that if I click on another fragment in the drawer, and then go back to the first fragment, the button and the text view are gone. in the first time this view pager fragment is creating the two other fragments. I think that when I go back to this fragment again by replacing it, it wont load the other two fragments and their text and button view. I'm going crazy because of this and I cant continue my work..what is wrong here?
I don't know how to upload code snippets..sorry
Thank you for your help :)
Is it possible to achieve interaction between fragments described in this image:
So, if one touches (or preferably swipes vertically) on fragment B, it shows (or is replaced by) fragment C and changes size to accommodate the larger fragment. Fragment C will contain a viewPager with additional fragments that are revealed on horizontal swipe (this part is done).
Fragment B will display the crucial contents of fragment C, which is why I want a separate fragment to do this. Fragment A takes user input and should always be interact-able.
I need help wrapping my head around how this should be implemented.
I have 2 Activities. In the second Activity I have a WebView in which I load a local html page(from the assets folder). In the onCreate method I call webView.loadUrl(url).
I need a slide in transition from the first Activity to the second. And this is where my problem comes in: The second activity slides in as it should, but it takes a moment to show the page in the WebView. So, basically, there's just a white empty screen that slides in.
I need the second Activity to load the html page before it slides in.
How do I do that?
An alternative would be to move the contents of your second activity (WebView etc) into a Fragment and push it into the existing Activity. That way you could preload the WebView into the fragment before showing it.
The short answer is: you can't really.
only one Activity is "active"(on the screen) at a time, because of this your SecondActivity cannot be doing anything while your FirstActivity is still visible.
You can achieve a similar effect as what you are after if you use only 1 Activity with 2 WebViews, one visible, and one hidden. You should be able to load a url in the second (hidden) webview while its hidden, and then make it visible and slide it in whenever you are ready.