WebViews inside ViewPager not always updating - android

I have the next scenario
An activity with a FrameLayout. In the FrameLayout I load fragments using replace
android.support.v4.app.Fragment contentToLoad = null;
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
...get the proper fragment depending of some logic
contentToLoad = new FragmentDemoContent();
fragmentTransaction.replace(R.id.fl_contentcontainer, contentToLoad);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
One of the fragments contains a ViewPager and a SlidingTabLayout (Copied from https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-SlidingTabLayout)
Inside the ViewPager I'm loading as pages other fragments, each fragment is a WebView that load a local content from assets. The adapter extends FragmentPagerAdapter
When I first load this fragment everything works fine, and I can swipe and load all the pages. But If I load other fragment and I came back to load the fragment with the ViewPager again, I get random blank pages, sometimes is the first one, sometime others. Sometimes also when I swipe back the previous blank page appears, but not always
I have tried, with no luck:
Check if its a WebView problem. It is not, using instead of the
fragments with the webviews blank default fragments with simply a
textview, I get the random blank pages too.
Invalidate the views, the pager... after each scroll or page changed
This approach ViewPager PagerAdapter not updating the View
Set http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit%28int%29
to load all the pages
Any ideas or something that we can try?

I figured out the problem. At the child fragment we were using getSupportFragmentManager instead of getChildFragmentManager. After replacing it it works ok.

Webview has rendering issue it takes time to load you can use textView with
myTextView.setText(Html.fromHtml("your html text"));

Related

Android Fragment Viewpager blinks/flickers when loading the fragments

I am using a viewpager to swipe between Fragments. However, the fragments blink/flash whenver I am loading the first fragment or any newly loaded fragment. any ideas ? I tried the HardwareAcceleration disable but nothing.

Fragment Transaction inside a fragment with a Tabbed actionbar not working

I want to be able to replace the current fragment which is loaded via the actionbar tabs using a ViewPager with another fragment that is not connected to any tab like this
getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(id,frag , "VideoEdit")
.commit();
however when I do this it loads the fragment all right but the tabs don't work anymore and everytime I click on a tab it just stays on this fragment. I managed to find this gist
https://gist.github.com/andreynovikov/4619215 but I'm not sure it's the same thing I'm looking for and I don't want to try it out for fear of messing up my existing code with it.
You should avoid transitions like that.
I'll recomend to use FragmentWrapper.
One more fragment that you placed inside Tab it contains only FrameLayout as a container for other fragments. In this fragment, you could use childFragmentManager to add current Fragment and replace it with VideoEdit.

Viewpager inside fragment and back button

I asked this question before getChildFragmentManager() and viewpager without any responses, probably because the explanation was too long.
Simply put I have a viewpager inside a fragment. The pages inside that viewpager need to be replaced through a series of screens. Pressing back on those screens will not exit the application. How do I do this?
challenges:
viewpager xml does not use framelayout. when I use the replace method, what container
id do I use?
when replacing the fragments why would I use getChildFragmentManager each time if that is private to the current fragment?
#goonedroid: that's not the case I'm looking for here. The only similarity is that the fragment has a viewpager. My viewpager's pages need to be replaced when clicked:
I have a navdrawer. Clicking item 2 shows FragmentA. Clicking FragmentA replaces it with FragmentB. Clicking FragmentB replaces it with FragmentC. Clicking FragmentC takes you back to FragmentB. Fragments are added to the backstack for proper back navigation.
Clicking item 1 in the navdrawer shows FragmentZ with a viewpager. The viewpagers pages are just FragmentA and all the aforementioned behavior. But going from A to B here, then pressing back shows no pages in the viewpager.
I finally figured out a solution, not sure why this works because I thought an Activity's getSupportFragmentManager() returns the same fragment manager as its fragment's getFragmentManager().
Anyway, instead of getFragmentManager() in the fragments, use getActivity().getFragmentManager()

How to replace Fragment inside ViewPager, using PagerAdapter?

My problem
I am using a ViewPager to display fragments inside a FragmentActivity. ViewPager gets fragments from the attached FragmentPagerAdapter.
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mAdapter = new HomePagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
Suppose ViewPager has 3 fragments to display: Fragment1, Fragment2, Fragment3. Fragment1 is a grid fragment and displays a grid. Fragment2 and Fragment3 have their own content to display.
When the use swipes on the screen, ViewPager will display the next fragment - Fragment2. And so on.
What I want?
What I want is that, when an item from the grid (displayed by Fragment1) is clicked, Fragment1 should be completely replaced with some other fragment, say Fragment4 (this is different fragment and is not returned by the attached adapter). User will work on Fragment4 and after ButtonBack click (just button iside Fragment4), Fragment1 with the grid should be displayed again. Meanwhile ViewPager should behave the same i.e on swipe, the next fragment (in our case Fragment2) will be displayed.
So I just want to get the same behavior as in example:
http://developer.android.com/training/basics/fragments/fragment-ui.html#Replace
My question
So, is this possible to achieve? If yes, then how to?
I would greatly appreciate for your help. Alex.
P.S. Sorry for my English:)
I've made a little example that shows how to achieve it:
https://github.com/danilao/fragments-viewpager-example
I think the point is to use another fragment as a container.
One way to achieve this is by adding another FragmentActivity that displays your Fragment4. Use Intent to start this activity and sent grid item position or other data in Extra.
On press of backbutton this activity will be finished and last activity with view pager will be displayed.
[EDIT] Old response. Better solutions are provided in other answers,
You will have to do the fragment transaction and add the existing fragment into backstack.
I created a sample project to show you how to do it.
The code is not fine tuned, I just created it to show you how to do it.
https://drive.google.com/folderview?id=0BxHClVwHSqq5dVRvT1Qyd0hYN0k&usp=sharing
But please be aware that even if you press back at any of the view pager screen, the previous fragment ends up showing as it is the same activity.
IF this is not expected behavior, then you should consider having multiple activities instead ( like tab layout )

FragmentPagerAdapter inside Fragment

I'm having a bit of trouble implementing a design based around multiple ViewPagers.
At a high level, I have a FragmentActivity with just a FrameLayout as it's content. I have 3 different Fragments that I want to display. All 3 are full screen and only 1 will be used at a time.
Fragment 1 is a basic fragment with some TextViews and ImageViews.
Fragment 2 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments.
Fragment 3 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments (that are different from Fragment 2)
In my FragmentActivity onCreate() I get the FragmentManager and begin a transaction to replace whatever is in my FrameLayout with a new instance of Fragment 2.
At this point everything is working as expected. My ViewPager in Fragment 2 works perfectly.
Now I have a menu option that replaces the Fragment 2 in my FrameLayout with a new instance of Fragment 3. This also works fine.
The problem arises when I try to put Fragment 2 back into the FrameLayout with another replace transaction. I see my PagerIndicater at the top, but my pages are blank.
I've tried just creating a new instance of my Fragment 2 and calling a replace transaction. I've also tried setting a tag on my Fragments when I call replace and adding a findFragmentByTag check before my replace instead of creating a new instance. Both gave me the same result of blank pages after my second replace.
For reference
My first design was simply a FragmentActivity with a ViewPager and a ViewIndicater. I only had Fragment 2 and Fragment 3 from my description above and a menu option to switch between them. To switch I had 2 different FragmentPagerAdapters defined and just called ViewPager.setAdapter to set the selected FragmentPagerAdapter. This was working perfectly, but now I need a new top level Fragment that isn't using ViewPager at all. This is why I decided to move my ViewPagers out into their own Fragments. My idea being that I would just swap in my fragments to a FrameLayout.
I don't have my code in front of me right now so I can't post any, but I'll add some code to my question tomorrow to help facilitate answers.
This question is a possible duplicate of Navigating back to FragmentPagerAdapter -> fragments are empty.
If your app can handle it (API 17), use getChildFragmentManager(). This problem seems to occur when using a Fragment to host your ViewPager and using FragmentPagerAdapter. Changing to FragmentStatePagerAdapter seemed to fix the problem as well, but I still think using getChildFragmentManager() is the smartest thing to do.
brockoli you can used not "good way". But for my it's worked.
You can use in view 2 layouts, where you bind fragments. First - for fragment with fragments. Second - for other fragments.
On replace fragment with fragments do not replace, but only change visibility first layout to gone and add new fragment to second layout. On back, remove fragment from second layout and set visibility for first layout to visible.

Categories

Resources