Activity托管的两个Fragment间的数据传递

2017-01-13  本文已影响0人  向着远方奔跑

废话不多说,直接上案例,以下案例截取自《Android编程权威指南第二版》第12章 对话框 12.3。

图1 CrimeFragment
图2 DatePickerFragment
如图1为CrimeFragment,图2为点击DETAILS下的第一个日历按钮弹出的日历选择器对话框,其为DatePickerFragment,这两个fragment是托管在一个CrimeActivity上的,现在要实现:
  1. 弹出的日历选择器要获取并显示按钮上的日期信息
  2. 用户在任意选择日期并点击OK后,按钮可以展示用户选择的日期

功能1解决方案(传递数据给DatePickerFragment)

实现思路

DatePickerFragment中新建newInstance(Date)方法,之后将Date作为argument附加给DatePickerFragment,然后在CrimeFragment中调用newInstance(Date)并将按钮日期传递进去,最后在DatePickerFragment中通过getArguments()方法获取Date即可

代码实现

public class DatePickerFragment extends DialogFragment {
      private static final String ARG_DATE = "date";

      public static DatePickerFragment newInstances(Date date) {
           Bundle args = new Bundle();       
           args.putSerializable(ARG_DATE, date); //ARG_DATE为标记键key,目的是在get时找到对应的值date   
           DatePickerFragment fragment = new DatePickerFragment();
           fragment.setArguments(args);
           return fragment;
      }
......
}
public class CrimeFragment extends Fragment {
      @Override
      public View onCreateView(...) {
            mDateButton.setOnClickListener(new View.OnClickListener() {    
                @Override    
                public void onClick(View v) {
                      FragmentManager fragmentManager = getFragmentManager();

                      DatePickerFragment dialog = DatePickerFragment.newInstances(mCrime.getDate());

                      dialog.show(fragmentManager, DIALOG_DATE);
                }
            }
       }
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
      Date date = (Date) getArguments().getSerializable(ARG_DATE);

      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      int year = calendar.get(Calendar.YEAR);
      int month = calendar.get(Calendar.MONTH);
      int day = calendar.get(Calendar.DAY_OF_MONTH);

      View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_date, null);

      mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_date_picker);
      mDatePicker.init(year, month, day, null);

      return new AlertDialog.Builder(getActivity())        
             .setView(v)        
             .setTitle(R.string.date_picker_title)  
             .setPositiveButton(android.R.string.ok, null)
             .create();
}

注:使用fragment的argument可以将接收来的数据保存在Bundle中,然后在使用时直接在fragment内部取出即可,不需要依赖外部的activity或者fragment,这可以使fragment的封装性更好,即低耦合,推荐使用。

功能2解决方案(返回数据给CrimeFragment)

实现思路

代码实现

public class CrimeFragment extends Fragment {

      private static final int REQUEST_DATE = 0;

      @Override
      public View onCreateView(...) {
            mDateButton.setOnClickListener(new View.OnClickListener() {    
                @Override    
                public void onClick(View v) {
                      FragmentManager fragmentManager = getFragmentManager();
                      DatePickerFragment dialog = DatePickerFragment.newInstances(mCrime.getDate());

                      dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE); //设置目标fragment

                      dialog.show(fragmentManager, DIALOG_DATE);
                }
            }
       }
}

REQUEST_DATE为请求代码,目标fragment可以通过请求代码确认是哪个fragment在回传数据

public class DatePickerFragment extends DialogFragment {
        public static final String EXTRA_DATE = "com.bignerdranch.android.criminalintent.date";
......
        private void sendResult(int resultCode, Date date) {    
                if (getTargetFragment() == null) {        
                    return;    
                }    
                Intent intent = new Intent();    
                intent.putExtra(EXTRA_DATE, date);
                getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
......
      return new AlertDialog.Builder(getActivity())        
                  .setView(v)        
                  .setTitle(R.string.date_picker_title)     
                  .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {            
                        @Override           
                        public void onClick(DialogInterface dialog, int which) {               
                              int year = mDatePicker.getYear();                
                              int month = mDatePicker.getMonth();               
                              int day = mDatePicker.getDayOfMonth();                
                              Date date = new GregorianCalendar(year, month, day).getTime(); 
               
                              sendResult(Activity.RESULT_OK, date);            
                        }        
                   })        
                   .create();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {    
      if (resultCode != Activity.RESULT_OK) {        
          return;    
      }    
      if (requestCode == REQUEST_DATE) {          
          Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE):
          mCrime.setDate(date);       
          updateDate();    
      }    
 }
private void updateDate() {    
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 EEEE", Locale.CHINA);
      mDateButton.setText(dateFormat.format(mCrime.getDate()));
}

手机屏幕空间有限,常常使用activity托管全屏的fragment界面,以显示用户输入要求。父Activity的fragment调用startActivityForResult()方法启动子Activity,子Activity销毁后,会将数据通过onActivityResult()传递给父Activity,父Activity会接收到onActivityResult()的调用请求,requestCode和resultCode核对正确后,父Activity会接收并将数据转发给自己托管的fragment

平板设备的屏幕空间比较大,适合以弹出对话框的方式显示信息和接收用户输入。这种情况,应设置目标fragment并调用对话框fragment的show方法。对话框销毁后,对话框的fragment会调用目标fragment的onActivityResult方法。

上一篇下一篇

猜你喜欢

热点阅读