-1
それぞれに1つのフラグメントを保持する2つのアクティビティがあります。 最初のfragmrntと2番目のアクティビティの間にデータ(単純な文字列)を送信しましたが、アクティビティから2番目のフラグメントに送ることはできません。 私は何が間違っていますか?2つのフラグメント間でデータを送信します。
これは、データを取得する活動です:
public class DiaryActivity extends AppCompatActivity {
private FragmentManager manager = getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diary);
if(savedInstanceState == null){
manager.beginTransaction().add(new EditDiaryFragment(), "tag").commit();
}
}
@Override
protected void onResume() {
super.onResume();
// get the data and pass it to the fradment... not working :(
Intent intent = getIntent();
String date = intent.getStringExtra("date");
String diary = intent.getStringExtra("diary");
Bundle bundle = new Bundle();
bundle.putString("date", date);
bundle.putString("diary", diary);
EditDiaryFragment editDiaryFragment = new EditDiaryFragment();
editDiaryFragment.setArguments(bundle);
}
}
そして、これは、データを取得する必要がありフラグメントである:
public class EditDiaryFragment extends Fragment implements View.OnClickListener {
private EditText diaryEditText;
private Button diarySaveButton;
private TextView diaryDateText;
private String today;
private ShvizoutDBHelper helper;
private StringBuffer buffer;
public EditDiaryFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helper = new ShvizoutDBHelper(getContext());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_edit_diary, container, false);
diaryDateText = (TextView) v.findViewById(R.id.diaryDateText);
diarySaveButton = (Button) v.findViewById(R.id.diarySaveButton);
diaryEditText = (EditText) v.findViewById(R.id.diaryEditText);
diarySaveButton.setOnClickListener(this);
Bundle bundle = getArguments();
if(bundle != null){
String bDate = bundle.getString("date");
String bDiary = bundle.getString("diary");
diaryDateText.setText(bDate);
diaryEditText.setText(bDiary);
}
return v;
}
}
感謝!!
データが失われる場所を確認するために、デバッグモードで実行するようにしてください。他のすべてが失敗した場合は、EventBusを使用してください。 – Vucko
明らかに、あなたは 'EditDiaryFragment'の2つのインスタンスを持っていて、1つは引数なしで、もう1つはインスタンスなしです。引数が – Selvin
mmのものは何もしません...どうすれば修正できますか?フラグメントをマネージャーに置き換えますか? – Roish