6

I've read around that nesting Fragments should be avoided (eg. here), but I can't see how to do the following:

I'm working on a tab application (android:minSdkVersion="12") with the following UI:

enter image description here

The search bar is always here and the user can navigate through several menu options ('home', 'gallery'...). My idea was to use a BaseActivity with a layout containing the search bar and a FrameLayout in which I would load the Fragment corresponding to the user navigation choice.

My issue is that in the 'Home' Fragment I have several tabs, which I wanted to implement the same way, i.e. with a layout containing the tab bar and a FrameLayout in which I would load the corresponding Fragment, and this leads to nested Fragment...

I know that instead of the BaseActivity I could use several activities and include the search bar in every layout, but it would make it appear and disappear every time the user would change activities...

EDIT

I also need a fixed footer, so I cannot use action bar as proposed by CommonsWare in his answer.

Anybody could help?

Community
  • 1
  • 1
jul
  • 36,404
  • 64
  • 191
  • 318

5 Answers5

3

There is Nested Fragments!!!!!!! in the new support library version(support library v4 , version 11)

drooooooid
  • 1,574
  • 1
  • 11
  • 17
2

You can use ViewPager and the FragmentPagerAdapter for this. ViewPager allows users to swipe between views or (in your case) Fragments. To show tablike-controls, use ViewPagerIndicator.

Using the layout you described, instead of loading a Fragment into the FrameLayout, inflate it with a layout like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/viewpagerIndicator"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

Next, assign a FragmentPagerAdapter to your ViewPager, which will then load your Fragments.

Also take a look at my answer here. It gives a more detailed example. Note however that it extends PagerAdapter, instead of the FragmentPagerAdapter you should be using.

Community
  • 1
  • 1
Reinier
  • 3,836
  • 1
  • 27
  • 28
0

Good news!!!!! android support library version 11 is already support nested fragment. Support Package, revision 11 (November 2012)

Changes for v4 support library:
    User Interface
        Added support for nested Fragment classes.

How to implement nested fragment, from android developer

You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page.

There are some key here: getChildFragmentManager() and getParentFragment()

Jay Parker
  • 631
  • 11
  • 23
0

Can't you do tab bars in BaseActivity layout? Then in BaseActivity layout will be search bar, tab bar and fragment frame. If you don't want nav bar in some fragment you can set visibility View.GONE to nav bar.

pjanecze
  • 3,145
  • 3
  • 21
  • 28
0

My idea was to use a BaseActivity with a layout containing the search bar and a FrameLayout in which I would load the Fragment corresponding to the user navigation choice.

If you use separate activities, rather than trying to force everything into a single activity, you will not wind up with nested fragments.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • But with several activities I cannot have the search box as a fixed header, right? I would need to include it to the layout of every activity and it will be reloaded everytime I change activities (i.e from the UI point of view it would disappear and reappear). Same thing if I create several activities for the tabs. – jul Mar 16 '12 at 13:37
  • 1
    @jul: "But with several activities I cannot have the search box as a fixed header, right?" -- put it at the top of each of the activities. Better yet, put it in the action bar. "I would need to include it to the layout of every activity and it will be reloaded everytime I change activities (i.e from the UI point of view it would disappear and reappear)" -- yes. – CommonsWare Mar 16 '12 at 13:45
  • Ok I'm going to check if I can customize the action bar as I want my search bar. I would need a fixed footer too, so if I can have two action bars and they can be customized, I'll use it. Thanks. – jul Mar 16 '12 at 13:52
  • @jul: You cannot have two action bars. – CommonsWare Mar 16 '12 at 13:55
  • Ok, so it seems I cannot achieve what I want: fixed header, fixed footer and within the Home, fixed tab menu. – jul Mar 16 '12 at 14:01
  • @jul: Not by using fragments all over the place. I am sure there are some terribly complicated ways of accomplishing your aims, but not using nested fragments. This is one of the reasons why you do not see complex apps using fixed headers and footers. Another is all the manual BACK button management that you would have to do. Yet another is making sure that you are not leaking memory (loosely-coupled separate activities make leaks less likely). – CommonsWare Mar 16 '12 at 14:04
  • "Not by using fragments all over the place." So, how can I do it? – jul Mar 16 '12 at 14:37