Android片段消失

时间:2017-01-06 06:53:13

标签: android android-fragments

上下文:

我添加了一张图片来直观地解释问题: click for image explanation

我有一个活动以及5个片段(主题,课程,添加课程和作业)

在Activity上我使用底部导航栏在Courses fragment和Assignments片段之间切换。

当我启动Activity时,Courses片段最初在屏幕上。 课程片段是一个主题列表,但最初这个片段有0个主题和一个按钮添加主题。当我单击Add Subject按钮时,AddCourse片段将替换Courses。一旦我在AddCourse片段中输入主题信息并单击确认,它就会创建一个新的Subject片段,并用AddCourse片段中填充的所有信息填充它,然后通过界面将其发送到Activity。 然后,Activity将主题发送到Courses片段并将其放在屏幕上(Courses片段中包含主题)。这非常有效。但是,当我切换到Assignments片段并返回Courses片段时,主题就消失了。

这里有一些代码:

Activity.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.SubMenuBuilder;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;

public class Activity extends AppCompatActivity implements AddCourseInterface {

    private Subject subject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                swapPage(tabId);
            }
        });

    }

    public void swapPage(@IdRes int tabId)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        if (tabId == R.id.tab_courses) {
            //if (subject != null) subject.print();
            ft.replace(R.id.content, Courses.newInstance(subject));
            ft.addToBackStack(null);
            ft.commit();
        }
        else if (tabId == R.id.tab_assignments) {
            ft.replace(R.id.content, Assignments.newInstance());
            ft.addToBackStack(null);
            ft.commit();
        }
        else if (tabId == R.id.tab_agenda) {
            ft.replace(R.id.content, Agenda.newInstance());
            ft.addToBackStack(null);
            ft.commit();
        }
        else if (tabId == R.id.tab_exams) {
            ft.replace(R.id.content, Exams.newInstance());
            ft.addToBackStack(null);
            ft.commit();
        }
        else if (tabId == R.id.tab_grades) {
            ft.replace(R.id.content, Grades.newInstance());
            ft.addToBackStack(null);
            ft.commit();
        }

    }

    @Override
    public void sendCourse(Subject subject) {
        this.subject = subject;
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content, Courses.newInstance(subject));
        ft.addToBackStack(null);
        ft.commit();
    }
}

AddCourse.java

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class AddCourse extends Fragment implements View.OnClickListener {

    private View inflatedView;
    private AddCourseInterface comm;

    private TextView nameText;
    private TextView nameGivenText;
    private TextView codeText;
    private TextView professorText;

    private CheckBox additionalCheck;
    private LinearLayout additionalInfo;
    private CheckBox monCheck;
    private LinearLayout monInfo;
    private TextView monLoc;
    private TextView monSTime;
    private TextView monETime;
    private CheckBox tueCheck;
    private LinearLayout tueInfo;
    private TextView tueLoc;
    private TextView tueSTime;
    private TextView tueETime;
    private CheckBox wedCheck;
    private LinearLayout wedInfo;
    private TextView wedLoc;
    private TextView wedSTime;
    private TextView wedETime;
    private CheckBox thuCheck;
    private LinearLayout thuInfo;
    private TextView thuLoc;
    private TextView thuSTime;
    private TextView thuETime;
    private CheckBox friCheck;
    private LinearLayout friInfo;
    private TextView friLoc;
    private TextView friSTime;
    private TextView friETime;

    private RadioButton mon_lec;
    private RadioButton mon_sem;
    private RadioButton mon_lab;

    private RadioButton tue_lec;
    private RadioButton tue_sem;
    private RadioButton tue_lab;

    private RadioButton wed_lec;
    private RadioButton wed_sem;
    private RadioButton wed_lab;

    private RadioButton thu_lec;
    private RadioButton thu_sem;
    private RadioButton thu_lab;

    private RadioButton fri_lec;
    private RadioButton fri_sem;
    private RadioButton fri_lab;

    private Button confirm;

    private String name = "Subject Name";
    private String givenName = "Short Name";
    private String code = "Course Code";
    private String professor = "Professor";

    private ArrayList<String> days = new ArrayList<String>();;
    private ArrayList<String> locations = new ArrayList<String>();
    private ArrayList<String> stimes = new ArrayList<String>();
    private ArrayList<String> etimes = new ArrayList<String>();
    private ArrayList<String> types = new ArrayList<String>();

    public AddCourse() {
        // Required empty public constructor
    }

    public static AddCourse newInstance() {

        return new AddCourse();
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflatedView = inflater.inflate(R.layout.fragment_add_course, container, false);

        nameText = (TextView) inflatedView.findViewById(R.id.subject_name);
        nameGivenText = (TextView) inflatedView.findViewById(R.id.short_name);
        codeText = (TextView) inflatedView.findViewById(R.id.course_code);
        professorText = (TextView) inflatedView.findViewById(R.id.professor);

        additionalCheck = (CheckBox) inflatedView.findViewById(R.id.check_additional);
        additionalCheck.setOnClickListener(this);

        additionalInfo = (LinearLayout) inflatedView.findViewById(R.id.additional_info);

        monCheck = (CheckBox) inflatedView.findViewById(R.id.check_mon);
        monCheck.setOnClickListener(this);

        tueCheck = (CheckBox) inflatedView.findViewById(R.id.check_tue);
        tueCheck.setOnClickListener(this);

        wedCheck = (CheckBox) inflatedView.findViewById(R.id.check_wed);
        wedCheck.setOnClickListener(this);

        thuCheck = (CheckBox) inflatedView.findViewById(R.id.check_thu);
        thuCheck.setOnClickListener(this);

        friCheck = (CheckBox) inflatedView.findViewById(R.id.check_fri);
        friCheck.setOnClickListener(this);

        monInfo = (LinearLayout) inflatedView.findViewById(R.id.monday_info);
        tueInfo = (LinearLayout) inflatedView.findViewById(R.id.tuesday_info);
        wedInfo = (LinearLayout) inflatedView.findViewById(R.id.wednesday_info);
        thuInfo = (LinearLayout) inflatedView.findViewById(R.id.thursday_info);
        friInfo = (LinearLayout) inflatedView.findViewById(R.id.friday_info);

        monLoc = (TextView) inflatedView.findViewById(R.id.monday_location);
        monSTime = (TextView) inflatedView.findViewById(R.id.monday_stime);
        monETime = (TextView) inflatedView.findViewById(R.id.monday_etime);

        tueLoc = (TextView) inflatedView.findViewById(R.id.tuesday_location);
        tueSTime = (TextView) inflatedView.findViewById(R.id.tuesday_stime);
        tueETime = (TextView) inflatedView.findViewById(R.id.tuesday_etime);

        wedLoc = (TextView) inflatedView.findViewById(R.id.wednesday_location);
        wedSTime = (TextView) inflatedView.findViewById(R.id.wednesday_stime);
        wedETime = (TextView) inflatedView.findViewById(R.id.wednesday_etime);

        thuLoc = (TextView) inflatedView.findViewById(R.id.thursday_location);
        thuSTime = (TextView) inflatedView.findViewById(R.id.thursday_stime);
        thuETime = (TextView) inflatedView.findViewById(R.id.thursday_etime);

        friLoc = (TextView) inflatedView.findViewById(R.id.friday_location);
        friSTime = (TextView) inflatedView.findViewById(R.id.friday_stime);
        friETime = (TextView) inflatedView.findViewById(R.id.friday_etime);

        mon_lec = (RadioButton) inflatedView.findViewById(R.id.mon_lec);
        mon_sem = (RadioButton) inflatedView.findViewById(R.id.mon_sem);
        mon_lab = (RadioButton) inflatedView.findViewById(R.id.mon_lab);

        tue_lec = (RadioButton) inflatedView.findViewById(R.id.tue_lec);
        tue_sem = (RadioButton) inflatedView.findViewById(R.id.tue_sem);
        tue_lab = (RadioButton) inflatedView.findViewById(R.id.tue_lab);

        wed_lec = (RadioButton) inflatedView.findViewById(R.id.wed_lec);
        wed_sem = (RadioButton) inflatedView.findViewById(R.id.wed_sem);
        wed_lab = (RadioButton) inflatedView.findViewById(R.id.wed_lab);

        thu_lec = (RadioButton) inflatedView.findViewById(R.id.thu_lec);
        thu_sem = (RadioButton) inflatedView.findViewById(R.id.thu_sem);
        thu_lab = (RadioButton) inflatedView.findViewById(R.id.thu_lab);

        fri_lec = (RadioButton) inflatedView.findViewById(R.id.fri_lec);
        fri_sem = (RadioButton) inflatedView.findViewById(R.id.fri_sem);
        fri_lab = (RadioButton) inflatedView.findViewById(R.id.fri_lab);

        mon_lec.setOnClickListener(this);
        mon_sem.setOnClickListener(this);
        mon_lab.setOnClickListener(this);

        tue_lec.setOnClickListener(this);
        tue_sem.setOnClickListener(this);
        tue_lab.setOnClickListener(this);

        wed_lec.setOnClickListener(this);
        wed_sem.setOnClickListener(this);
        wed_lab.setOnClickListener(this);

        thu_lec.setOnClickListener(this);
        thu_sem.setOnClickListener(this);
        thu_lab.setOnClickListener(this);

        fri_lec.setOnClickListener(this);
        fri_sem.setOnClickListener(this);
        fri_lab.setOnClickListener(this);

        confirm = (Button) inflatedView.findViewById(R.id.button_confirm_subject);
        confirm.setOnClickListener(this);

        return inflatedView;
    }

    @Override
    public void onClick(View v) {
        if (v == additionalCheck)
        {
            if (additionalCheck.isChecked())
            {
                System.out.println("ADDITIONAL INFO ENABLED <---------------------");
                additionalInfo.setVisibility(View.VISIBLE);
            }
            else
            {
                System.out.println("ADDITIONAL INFO DISABLED <---------------------");
                additionalInfo.setVisibility(View.GONE);
            }
        }

        else if (v == monCheck)
        {
            if (monCheck.isChecked())
            {
                System.out.println("MONDAY WAS CHECKED <---------------------");
                monInfo.setVisibility(View.VISIBLE);
            }
            else
            {
                System.out.println("MONDAY WAS UNCHECKED <---------------------");
                monInfo.setVisibility(View.GONE);
            }
        }

        else if (v == tueCheck)
        {
            if (tueCheck.isChecked())
            {
                System.out.println("tueDAY WAS CHECKED <---------------------");
                tueInfo.setVisibility(View.VISIBLE);
            }
            else
            {
                System.out.println("tueDAY WAS UNCHECKED <---------------------");
                tueInfo.setVisibility(View.GONE);
            }
        }

        else if (v == wedCheck)
        {
            if (wedCheck.isChecked())
            {
                System.out.println("wedDAY WAS CHECKED <---------------------");
                wedInfo.setVisibility(View.VISIBLE);
            }
            else
            {
                System.out.println("wedDAY WAS UNCHECKED <---------------------");
                wedInfo.setVisibility(View.GONE);
            }
        }

        else if (v == thuCheck)
        {
            if (thuCheck.isChecked())
            {
                System.out.println("thuDAY WAS CHECKED <---------------------");
                thuInfo.setVisibility(View.VISIBLE);
            }
            else
            {
                System.out.println("thuDAY WAS UNCHECKED <---------------------");
                thuInfo.setVisibility(View.GONE);
            }
        }

        else if (v == friCheck)
        {
            if (friCheck.isChecked())
            {
                System.out.println("friDAY WAS CHECKED <---------------------");
                friInfo.setVisibility(View.VISIBLE);
            }
            else
            {
                System.out.println("friDAY WAS UNCHECKED <---------------------");
                friInfo.setVisibility(View.GONE);
            }
        }

        else if (v == mon_lec) {
            mon_lec.setChecked(true);
            mon_sem.setChecked(false);
            mon_lab.setChecked(false);
        }

        else if (v == mon_sem) {
            mon_lec.setChecked(false);
            mon_sem.setChecked(true);
            mon_lab.setChecked(false);
        }

        else if (v == mon_lab) {
            mon_lec.setChecked(false);
            mon_sem.setChecked(false);
            mon_lab.setChecked(true);
        }

        else if (v == tue_lec) {
            tue_lec.setChecked(true);
            tue_sem.setChecked(false);
            tue_lab.setChecked(false);
        }

        else if (v == tue_sem) {
            tue_lec.setChecked(false);
            tue_sem.setChecked(true);
            tue_lab.setChecked(false);
        }

        else if (v == tue_lab) {
            tue_lec.setChecked(false);
            tue_sem.setChecked(false);
            tue_lab.setChecked(true);
        }

        else if (v == wed_lec) {
            wed_lec.setChecked(true);
            wed_sem.setChecked(false);
            wed_lab.setChecked(false);
        }

        else if (v == wed_sem) {
            wed_lec.setChecked(false);
            wed_sem.setChecked(true);
            wed_lab.setChecked(false);
        }

        else if (v == wed_lab) {
            wed_lec.setChecked(false);
            wed_sem.setChecked(false);
            wed_lab.setChecked(true);
        }

        else if (v == thu_lec) {
            thu_lec.setChecked(true);
            thu_sem.setChecked(false);
            thu_lab.setChecked(false);
        }

        else if (v == thu_sem) {
            thu_lec.setChecked(false);
            thu_sem.setChecked(true);
            thu_lab.setChecked(false);
        }

        else if (v == thu_lab) {
            thu_lec.setChecked(false);
            thu_sem.setChecked(false);
            thu_lab.setChecked(true);
        }

        else if (v == fri_lec) {
            fri_lec.setChecked(true);
            fri_sem.setChecked(false);
            fri_lab.setChecked(false);
        }

        else if (v == fri_sem) {
            fri_lec.setChecked(false);
            fri_sem.setChecked(true);
            fri_lab.setChecked(false);
        }

        else if (v == fri_lab) {
            fri_lec.setChecked(false);
            fri_sem.setChecked(false);
            fri_lab.setChecked(true);
        }
        else if (v == confirm)
        {

            name = nameText.getText().toString();
            givenName = nameGivenText.getText().toString();
            code = codeText.getText().toString();
            professor = professorText.getText().toString();

            if (monCheck.isChecked())
            {
                days.add("monday");
                locations.add(monLoc.getText().toString());
                stimes.add(monSTime.getText().toString());
                etimes.add(monETime.getText().toString());
                if (mon_lec.isChecked()) {
                    types.add("lec");
                }
                else if (mon_sem.isChecked()) {
                    types.add("sem");
                }
                else if (mon_lab.isChecked()) {
                    types.add("lab");
                }
            }
            if (tueCheck.isChecked())
            {
                days.add("tuesday");
                locations.add(tueLoc.getText().toString());
                stimes.add(tueSTime.getText().toString());
                etimes.add(tueETime.getText().toString());
                if (tue_lec.isChecked()) {
                    types.add("lec");
                }
                else if (tue_sem.isChecked()) {
                    types.add("sem");
                }
                else if (tue_lab.isChecked()) {
                    types.add("lab");
                }
            }
            if (wedCheck.isChecked())
            {
                days.add("wednesday");
                locations.add(wedLoc.getText().toString());
                stimes.add(wedSTime.getText().toString());
                etimes.add(wedETime.getText().toString());
                if (wed_lec.isChecked()) {
                    types.add("lec");
                }
                else if (wed_sem.isChecked()) {
                    types.add("sem");
                }
                else if (wed_lab.isChecked()) {
                    types.add("lab");
                }
            }
            if (thuCheck.isChecked())
            {
                days.add("thursday");
                locations.add(thuLoc.getText().toString());
                stimes.add(thuSTime.getText().toString());
                etimes.add(thuETime.getText().toString());
                if (thu_lec.isChecked()) {
                    types.add("lec");
                }
                else if (thu_sem.isChecked()) {
                    types.add("sem");
                }
                else if (thu_lab.isChecked()) {
                    types.add("lab");
                }
            }
            if (friCheck.isChecked())
            {
                days.add("friday");
                locations.add(friLoc.getText().toString());
                stimes.add(friSTime.getText().toString());
                etimes.add(friETime.getText().toString());
                if (fri_lec.isChecked()) {
                    types.add("lec");
                }
                else if (fri_sem.isChecked()) {
                    types.add("sem");
                }
                else if (fri_lab.isChecked()) {
                    types.add("lab");
                }
            }

            /*
            System.out.println(name);
            System.out.println(givenName);
            System.out.println(code);
            System.out.println(professor);

            System.out.println(days);
            System.out.println(locations);
            System.out.println(stimes);
            System.out.println(etimes);
            System.out.println(types);
            */

            Subject subject = Subject.newInstance(name, givenName, code, professor, days, locations, stimes, etimes, types);
            comm = (AddCourseInterface) getActivity();
            comm.sendCourse(subject);
        }

    }
}

Courses.java

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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 android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class Courses extends Fragment implements View.OnClickListener {

    //private static final String ARG_PARAM1 = "arg1";

    private String title;
    private TextView titleView;
    private ArrayList<Subject> subjects;
    private Subject subject;
    private View inflatedView;
    private Button addSubject;

    public Courses() {
        // Required empty public constructor
    }

    public static Courses newInstance(Subject s) {
        Courses courses = new Courses();

        Bundle bundle = new Bundle();
        bundle.putSerializable("sub1", s);
        courses.setArguments(bundle);

        return courses;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        subjects = new ArrayList<Subject>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflatedView = inflater.inflate(R.layout.fragment_courses, container, false);

        titleView = (TextView) inflatedView.findViewById(R.id.courses_title);

        addSubject = (Button) inflatedView.findViewById(R.id.add_subject);
        addSubject.setOnClickListener(this);

        subject = (Subject) getArguments().getSerializable("sub1");

        if (subject != null) addSubject(subject);

        return inflatedView;
    }

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

    public void changeText(String t)
    {
        titleView.setText(t);
    }

    public void addSubject(Subject s)
    {
        subjects.add(s);
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.sub1, s);
        ft.addToBackStack(null);
        ft.commit();
        System.out.println("SUBJECT ADDED TO COURSES < ------------------------");
        //subject.print();
    }

    @Override
    public void onClick(View v) {
        System.out.println("BUTTON CLICKED < -----------------------------------");
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content, AddCourse.newInstance());
        ft.addToBackStack(null);
        ft.commit();
    }
}

正如您所看到的,当我在AddCourse中单击CONFIRM时,主题片段在Courses片段中正确显示,但是一旦我转到Assignments并返回,即使我使用相同的课程创建新的课程实例,主题也会消失主题(参考保存在我的活动中)。任何想法?

我添加了一张图片来直观地解释问题: click for image explanation

1 个答案:

答案 0 :(得分:0)

重新创建课程片段以了解数据丢失的原因,您应该将日期保存到sqlite或sharedpreference并从那里读取数据而不是将数据传递给活动