I know my questions sounds like a tongue twister. Anyway I wanted to implement it but I don't have any idea on how to do it. I already know on how to use a FragmentActivity and add the TabHost for it but now what I have is a fragment that should also contain a Tab which also has a fragment. To give it a more detail here's what I wanted to implement:
What I tried so far is to Create a new FragmentActivity which will be contained inside a FragmentActivity and I got a ClassCastException. Any ideas on how can I achieve this?
Using a newer version of the Support Library, or native Fragments if you only care about Honeycomb, you can add Fragments as children of other fragments.
You can call getChildFragmentmanager() inside one of your top level Fragments and add your child Fragments there.
Related
I want to make tabView inside a fragment. I have template from this and I want to place tabView inside one fragment. Can I implement this like implementation using activity? Any tutorial or advice about this case? Thank you.
Yes. You can use ViewPager and TabLayout
(see http://developer.android.com/training/animation/screen-slide.html)
There are two ways to add the tabs inside fragment:
TabLayout: Using this layout you can add tab in your fragment and also latest approach provided by android.
Tabhost- Using old tabhost layout. This approach is a depreciated approach. However it will also work with fragment properly.
For more reference you can check my example on GitHub-Material Navigation
I am developping an app with an activity which switches between a few fragments. I was wondering if it is possible to have one of this fragment with tabs in the action bar, but not the other ones, knowing that, in general, the activity is tabbed and the tabs switch between fragments.
In a nutshell, I want an activity with a few fragments, and one of this fragments should have tabs to browse between other fragments, is that possible?
Ofcourse it is possible, take a look around StackOverflow, there are a couple of questions already.
Instead of normal TabHost your should use FragmentTabHost and because you will have fragments inside a fragment, you will have to use getChildFragmentManager() instead of getFragmentManager().
Adding tab inside fragment
Nice post Marko! I was just typing up basically the same answer. Here is a link for the documentation on Nested Fragments hope that helps as well
I am trying to build an activity that employs a ViewPager (FragmentPagerAdapter). Inside the ViewPager, I would like to open fragments programmatically. I know this can easily be achieved using FragmentTransaction.replace(int, Fragment), but I would like to use my own Fragment object. I know you can do that if you use android.app.FragmentTransaction.replace, but unfortunately from what I have found so far you can only use the support package (android.support.v4.app.FragmentTransaction) because the ViewPager originates in the support package.
Are there any workarounds/alternatives? Is there something like ViewPager in Android.view or something? Or anything that can produce tabs at the top and have it swipeable like the ViewPager (FragmentPagerAdapter)?
Really I guess the question I'm asking is if there is a way to use my own Fragment (e.g. MyFragment extends Fragment and use "MyFragment" as a fragment in a ViewPager).
Thanks for the help in advance!
use the support library and use the FragmentActivity class along with the support Fragment class android.support.v4.app.Fragment to use fragments in your viewpager
you will have to change all your fragments you want to use in the viewpager over to the support fragment
Let me describe my project.
I use actionbarsherlock with SlidingMeno from jfeinstein10 . On my main activity i have this sliding menu with a couple of menu item that open Fragments in container. (the same as basic fragment sample in sliding menu example) In one of those fragments i have view-pager with viewpageindicator. View-pager is tied up to FragmentStatePagerAdapter with my fragments. So i have fragments inside fragments. I use getChildFragmentManager() like suggested in the doc, when using fragments inside fragments.
When i select menu item with fragment that hold view-pager for the first time everything work like it should. But when i select some other menu item and then go back to this view-pager fragment i get this exception.
All other fragments don’t have this problem and in stack trace there is no reference to my code.
https://gist.github.com/4502038
Did any one encounter similar problem or have some idea what is going on.
EDIT: If i use getFragmentManager() instead of getChildFragmentManager() even thou i'm in subfragment. I don't get an exception, but one of the fragments inside viewpager doesn't get recreated after i select viewpager fragment for the second time.
I created a project on github so you guys can take a look.
https://github.com/pzagor2/TestErrorApp
See this: https://code.google.com/p/android/issues/detail?id=42601 bug.
Uncommenting addToBackStack https://github.com/pzagor2/TestErrorApp/blob/master/MainActivity/src/com/example/myapp/MainActivity.java#L69 , worked for me with your project setup.
You can see the bug in the Google bug report in the first URL.
Hope this helped.
I just looked at the GitHub project and it looks like you have not commited your code yet. But I assume your ViewPager is in a fragment itself and the ViewPager is hosting child fragments. If that's so, then I would recommend you test to make sure your FragmentStatePagerAdapter is constructed with the result of getChildFragmentManager().
I need your help.
I have one Activity with two fragments: one fragment with simple TextView in LinearLayout and other fragment: ViewPager with 3 fragments in FragmentPagerAdapter.
I make transaction with replace action, but I have error from ViewPager: java.lang.IllegalStateException: Recursive entry to executePendingTransactions.
That's a source code that show this problem, maybe anybody knows how to fix it.
Um no. A ViewPager extends ViewGroup. How does that make it a Fragment?
Yes, you're right that nesting Fragments isn't supported. However, it works if you have a Fragment holding a ViewPager whose Adapter contains several Fragments. Trust me, it works. I'm using it in the current project I'm working on right now and I've even got nested ViewPagers without the horizontal touch events fighting for control, so essentially I have Fragments within a Fragment.
With revision 11 of the Android Support Library, you can now nest fragments within fragments to avoid the recursive exception. See this question How to add a Fragment inside a ViewPager using Nested Fragment (Android 4.2)
A ViewPager is basically a Fragment, and you can't put Fragments in Fragments. Period.