コンテキスト消える:Androidの断片は
Iは、視覚的に問題を説明するための画像を追加した: click for image explanation
Iが活性ならびに5つのフラグメント(件名、コース、AddCourse及び割り当て)を有する
アクティビティでは、下部のナビゲーションバーを使用して、コースの断片と割り当ての断片を切り替えます。
アクティビティを開始すると、コースの断片は最初は画面に表示されます。 コースの断片は科目のリストであることを意図していますが、最初にこの断片には0件の科目があり、科目を追加するボタンがあります。 Add Subjectボタンをクリックすると、AddCourseフラグメントがCoursesを置き換えます。 AddCourseフラグメント内の件名情報を入力して確認をクリックすると、新しいSubjectフラグメントが作成され、AddCourseフラグメントに埋め込まれたすべての情報が埋め込まれた後、インタフェースを介してアクティビティに送信されます。 次に、アクティビティは、科目を科目の断片に送信し、それを画面に表示します(科目の中に科目がある科目の断片)。これは素晴らしいです。しかし、課題の断片に切り替えてコースの断片に戻ったとき、その科目は消えてしまいました。
ここにいくつかのコードです:
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で「確認」をクリックすると表示されるように、件名の断片がコースの断片の中に正しく表示されますが、割り当てを受けて戻ってくると、コースの新しいインスタンスを作成していても件名はなくなります。同じ主題(私の活動で保存された参照)。何か案は?
私は視覚的に問題を説明するための画像を追加しました: click for image explanation
私は毎回同じ対象にする必要はありません毎回同じ件名を渡して再作成して以来? Courses.javaで見ることができるように、適切なサブジェクトフラグメントが追加されるかどうかを確認するために2つのプリントがあります:System.out.println( "サブジェクトに追加されたサブジェクト<---------------- -------- "); subject.print();それはそうであるようです。私はそれがなぜ表示されないのか理解できないのです –
あなたの質問を得ていない –
コースのタブをクリックする度に、アクティビティから渡された主題を使ってコースの断片が再現されます。もし私がそれを作り直すたびに、私は同じ主題を渡すのはいつも同じではないのですか? –