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
extendsandroid.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.