以编程方式添加的片段消失

时间:2013-07-10 15:00:44

标签: android android-fragments fragment

我的应用程序使用选项卡,每个选项卡都是Fragment(来自android.app.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);
    }
}

}

它使用FragmentTransaction以编程方式添加第一个片段。它实际上工作(我的意思是,嵌套片段被添加,我可以看到它)但每次我切换到另一个选项卡(显示另一个片段)并返回到它,嵌套片段已消失。

以下是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;
    }
  • 嵌套片段CalendarFragment扩展android.support.v4.app.Fragment
  • 我的活动是FragmentActivity

我甚至尝试留在这个片段上,将活动放在后台(使用主页按钮)并恢复它,仍然显示嵌套片段。当我在我的应用程序的选项卡之间切换时,它确实发生了。

1 个答案:

答案 0 :(得分:0)

不是在onCreate中添加只在创建片段时调用的片段,而是在onCreateView中创建它,但检查片段是否为空

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);
    }
}