我可以重用DialogFragment实例,还是应该为DatePickerDialog创建一个新实例?

时间:2015-09-30 03:18:33

标签: android android-fragments instance android-datepicker

因此,当DatePickerFragment为空时,我为用户创建EditText以在EditText行上输入日期:

@Override
public void onClick(View v) {
   if (xEditText.getText().length() == 0) {
   DatePickerFragment newFragment = new DatePickerFragment();
   newFragment.show(getSupportFragmentManager(), "datePicker");
}

稍后,如果EditText不为空(因为用户先前输入了日期),那么我想将包中的日期发送到片段:

else {
     ...        
     Bundle argsbundle = new Bundle();
     argsbundle.putInt("year", c.get(Calendar.YEAR));
     ...

我的问题是我应该在DatePickerFragment声明中使用之前命名的newFragment创建else {的新实例:

else {
     ...        
     Bundle argsbundle = new Bundle();
     argsbundle.putInt("year", c.get(Calendar.YEAR));
     DatePickerFragment newFragment = new DatePickerFragment();
     newFragment.setArguments(argsbundle);
     newFragment.show(getSupportFragmentManager(), "datePicker");

或者我应该创建一个新命名的片段,与newFragment不同:

     else {
     ...        
     Bundle argsbundle = new Bundle();
     argsbundle.putInt("year", c.get(Calendar.YEAR));
     DatePickerFragment secondFragment = new DatePickerFragment();
     secondFragment.setArguments(argsbundle);
     secondFragment.show(getSupportFragmentManager(), "datePicker");

0 个答案:

没有答案