1

My application uses tabs, each tab is Fragment (from android.app.Fragment). I want to use nested fragments in one of those.

Here is the top level fragment supposed to contains the nested fragment:

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import fr.epitech.test_esi.R;

public class ReservationCalendarFragment extends Fragment {
    public static final int stringId = R.string.calendar_tab;
    public static final String tag = "reservation_calendar_frag";

    CalendarFragment t = new CalendarFragment();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fm = ((FragmentActivity) getActivity())
                .getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.calendarframelayout, t, CalendarFragment.tag);
        ft.commit();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.reservation_calendar_layout, null);
    }
}

}

It adds programmatically the first fragment using a FragmentTransaction. It actually works (i mean, the nested fragment is added and i can see it) but each time i switch to another tab (so another fragment is displayed) and go back to it, the nested fragment has disappeared.

Here is the code of the CalendarFragment:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import fr.epitech.test_esi.R;

public class CalendarFragment extends Fragment {

    public static final String tag = "calendar_frag";

    private SimpleDateFormat _df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View fragView = inflater.inflate(R.layout.calendar_layout, null);
        CalendarView calendarView = (CalendarView) fragView
                .findViewById(R.id.calendar);
        calendarView.setOnDateChangeListener(new OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year,
                    int month, int dayOfMonth) {
                Calendar c = Calendar.getInstance();

                c.set(year, month, dayOfMonth);
                Log.d("DATE", c.getTime().toString());
            }
        });
        return fragView;
    }
  • The nested fragment CalendarFragment extends android.support.v4.app.Fragment
  • My activity is a FragmentActivity

I even tried stay on this fragment, put the activity in background (using home button) and resume it, the nested fragment is still displayed. It's really happen when i switch between the tabs of my application.

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
nathan
  • 1,111
  • 3
  • 18
  • 33
  • 1
    When you want nested fragments you use `getChildFragmentmanager()`. `R.id.calendarframelayout` is the id of a view from `R.layout.reservation_calendar_layout` or is the id of the layout where the tabs are positioned? Also, simple tabs or tabs implemented through a `ViewPager`? – user Jul 10 '13 at 15:10
  • @Luksprog i use tabs in a ViewPager. The R.id.calendarframelayout is the id of the framelayout contained in R.layout.reservation_calendar_layout. Also, how should i use `getChildFragmentManager`? It seems like i can't use it with Fragment from support lib. – nathan Jul 10 '13 at 15:30
  • Check this answer of mine http://stackoverflow.com/a/13381792/493939 and see if it helps you. – user Jul 10 '13 at 16:38
  • @Luksprog atm i'd to be able to use getChildFragmentManager :) i'm working on this. – nathan Jul 10 '13 at 17:01

1 Answers1

0

Instead of adding the fragment in onCreate which would only be called once the fragment is created, create it in onCreateView but check if the fragment is null first

public class ReservationCalendarFragment extends Fragment {
    public static final int stringId = R.string.calendar_tab;
    public static final String tag = "reservation_calendar_frag";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        CalendarFragment t = (CalendarFragment) getChildFragmentManager()
            .findFragmentByTag(CalendarFragment.tag);

        if(t == null) {
            t = new CalendarFragment();
        }
        FragmentTransaction transaction = getChildFragmentManager()
                .beginTransaction();
        ft.add(R.id.calendarframelayout, t, CalendarFragment.tag);
        ft.commit();
        return inflater.inflate(R.layout.reservation_calendar_layout, null);
    }
}
  • java.lang.NoSuchMethodError: fr.epitech.test_esi.fragments.ReservationCalendarFragment.getChildFragmentManager 07-10 17:34:54.321: E/AndroidRuntime(16917): at fr.epitech.test_esi.fragments.ReservationCalendarFragment.onCreateView(ReservationCalendarFragment.java:27) – nathan Jul 10 '13 at 15:35
  • I actually use support library v4. – nathan Jul 10 '13 at 15:37
  • I trying using support library v13 but still the same. – nathan Jul 10 '13 at 16:13