-1
我有兩個活動,每個活動都有一個片段。 我發送了第一個fragmrnt到第二個activity之間的數據(簡單字符串),但我無法將它從活動發送到第二個片段。 我在做什麼錯?在兩個片段之間發送數據
這是獲取數據的活動:
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'實例,其中一個帶有參數,第二個沒有......並且你沒有對帶有參數 – Selvin
毫米的那個進行任何操作......我該如何解決它?用經理替換片段? – Roish