Activity和Fragment之间的数据传递

Posted 原来如此丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activity和Fragment之间的数据传递相关的知识,希望对你有一定的参考价值。

一.Fragment向它所依附的Activity传递数据,步骤:    1.在Fragmnet中定义一个接口,该接口需要activity去实现,类似定义下面这种接口,参数就是我们想传递给activity:
   
    
      //回调接口用于传递年月给activity
    
    
         public interface OnDateGetListener
    
    
             void onDateGet(int displayYear,int displayMonth);
    
    
         
    
   
   2.Activity去实现该接口
   
    
     public class ParentAttentionActivity implements HomeworkReportFragment.OnDateGetListener
    
    
      
    
    
     //接收homefragment的数据回调
    
    
         @Override
    
    
         public void onDateGet(int displayYear, int displayMonth) 
    
    
             this.displayMonth = displayMonth;
    
    
             this.displayYear = displayYear;
    
    
      
    
    
             Log.e("关心disMonth + dispYear",displayYear + " " + displayMonth);
    
    
         
    
   
   3.还需要在Fragment中做一些处理才能保证不出异常
   
    
      @Override
    
    
         public void onAttach(Activity activity) 
    
    
             super.onAttach(activity);
    
    
             try 
    
    
                 onDateGetListener = (OnDateGetListener) activity;
    
    
              catch (ClassCastException e) 
    
    
                 throw new ClassCastException(activity.toString() + " must implementonDateGetListener");
    
    
             
    
    
         
    
   
   在Fragment的onAttach()方中就可以获取所依附的Activity实现的接口,之后获得这个对象之后可以进行传递参数:
   
    
         private OnDateGetListener onDateGetListener;
    
    
         private int displayMonth;
    
    
         private int displayYear;
    
    
         @Override
    
    
         public void onResume() 
    
    
             Bundle bundle = getActivity().getIntent().getExtras();
    
    
             if(bundle != null) 
    
    
                 displayMonth = bundle.getInt("displayMonth");
    
    
                 displayYear = bundle.getInt("displayYear");
    
    
                 if (onDateGetListener != null) //回传数据给activity
    
    
                     onDateGetListener.onDateGet(displayYear, displayMonth);
    
    
                 
    
    
             
    
    
             super.onResume();
    
    
         
    
   
   按照个人需求在不同情况下传递传递参数即可   以上为自己用到的Fragment向Acticity传参
二.Activity向Fragment传递参数 讲道理来说,需要分成两种情况:
1.有关联的Activity向Fragment传递参数: 在该Activity中获取Fragment对象,然后利用fragment.setArguments(Bundle bundle)传参 在Fragment中通过this.getArguments()获取参数 2. 无关联的Activity向Fragment传递参数: 我们可以通过在不关联的Activity向关联的Activity进行 startActivity(new Intent()),而在Fragment中getActivity().getIntent().getExtras()即可获取到传递的参数
   
    
     public class EarlierReportActivity 
    
    
                       
    
    
          Intent intent = new  Intent(EarlierReportActivity.this,ParentAttentionActivity.class);
    
    
                         Bundle bundle = new Bundle();
    
    
                         bundle.putString("dateDesc", dateDesc);
    
    
                         bundle.putString("numWeekend",numWeekend);
    
    
                         bundle.putInt("displayMonth", displayMonth);
    
    
                         bundle.putInt("displayYear", displayYear);
    
    
                         bundle.putInt("position",prePosition);
    
    
                         intent.putExtras(bundle);
    
    
                         startActivity(intent);
    
    
     
    
   
    
     
       public class WeaknessFragment implementsWeaknessView<WeaknessBean> 
     
     
              Bundle bundle = getActivity().getIntent().getExtras();
       
     
     
              if(bundle != null)
     
     
                  int displayMonth = bundle.getInt("displayMonth");
     
     
                  int displayYear = bundle.getInt("displayYear");
     
     
                  int position = bundle.getInt("position");
     
     
                  String dateDesc = bundle.getString("dateDesc");
     
     
       
     
     
                  long[] week = WeekUtils.dealWithDateDesc(displayYear, displayMonth, position);//上周星期一星期日数据
     
     
                  lastMondoy = week[0];
     
     
                  lastSunday = week[1];
     
     
                  presenter.loadSujectErrorFromServer(subjectIds[0], lastMondoy, lastSunday);
     
     
              else
     
     
                  //默认展示上周数据
     
     
                  long[] lastWeekend = WeekUtils.getLastWeekend();
     
     
                  lastMondoy = lastWeekend[0];
     
     
                  lastSunday = lastWeekend[1];
     
     
                  presenter.loadSujectErrorFromServer(subjectIds[0], lastMondoy, lastSunday);
     
     
              
     
    

以上是关于Activity和Fragment之间的数据传递的主要内容,如果未能解决你的问题,请参考以下文章

Activity和Fragment之间的数据传递

activity与fragment之间的传递数据

fragment之间的数据传递

fragment的数据传递

fragment互相间的数据传递

fragment和activity之间进行数据交互