Android中Activity与Fragment之间的传值问题

Activity与Fragment之间的传值问题

Activity传值给Fragment

添加Fragment的Activity:

 Fragment01 fragment = new Fragment01();
        Bundle bundle = new Bundle();
        bundle.putString("str","这是Activity传来的值");
        fragment.setArguments(bundle);

        fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fm01id,fragment01);
        fragmentTransaction.commit();
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Fragment中接受Activity传过来的值:

String str = (String)getArguments().get(“str”);
   
   
  • 1

Fragment传值给Activity:

实现思路:
1.在Fragment中写一个回调接口
2.在activity中实现这个回调接口
3.在Fragment中onAttach 方法中得到activity中实现好的 实例化接口对象
4.用接口的对象 进行传值

Activity:

public class MainActivity extends Activity implements CallBackValue{  

    private TextView tv1;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        tv1 = (TextView) findViewById(R.id.tv1);  

        FragmentManager manager = getFragmentManager();  
        FragmentTransaction transaction =   manager.beginTransaction();  
        transaction.add(R.id.contents, new Fragmen1());  
        transaction.commit();  

    }  
    //要获取的值  就是这个参数的值  
    @Override  
    public void SendMessageValue(String strValue) {  
        // TODO Auto-generated method stub  
        tv1.setText(strValue);  
    }  
} 
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Fragment:

public class Fragmen1 extends Fragment{  
    private Button btn1;  
    private EditText et1;  
    CallBackValue callBackValue;  

    /** 
     * fragment与activity产生关联是  回调这个方法  
     */  
    @Override  
    public void onAttach(Activity activity) {  
        // TODO Auto-generated method stub  
        super.onAttach(activity);  
        //当前fragment从activity重写了回调接口  得到接口的实例化对象  
        callBackValue =(CallBackValue) getActivity();  
    }  


    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        View view = inflater.inflate(R.layout.fragment_layout1, container, false);  
         btn1 = (Button) view.findViewById(R.id.btn1);  
         et1 = (EditText) view.findViewById(R.id.et1);  
         btn1.setOnClickListener(new OnClickListener() {  

            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                String strValue = et1.getText().toString().trim();  
                callBackValue.SendMessageValue(strValue);  

            }  
        });  

        return view;  
    }  
    //定义一个回调接口  
    public interface CallBackValue{  
        public void SendMessageValue(String strValue);  
    }  
}  
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

onAttach的解决方案 :

今天在写一个Fragment的Demo中,在onAttach中进行赋值工作,结果显示并没有赋值,意味着onAttach并没有调用,最终在stackFlow找到了答案:


It’s not called because this method has been added in API 23. If you run your application on a device with API 23 (marshmallow) then onAttach(Context) will be called. On all previous Android Versions onAttach(Activity) will be called.


我是看  onAttach(Activity) 方法被deprecation了,所以用最新的 onAttach(Context) 方法,哪想到在API低于 23 的版本中不会去调用后者,只会去调用onAttach(Activity)。下面是看到的一个比较好的解决方案:



    
    
  1. /*
  2. * onAttach(Context) is not called on pre API 23 versions of Android and onAttach(Activity) is deprecated
  3. * Use onAttachToContext instead
  4. */
  5. @TargetApi( 23)
  6. @Override
  7. public void onAttach(Context context) {
  8. super.onAttach(context);
  9. onAttachToContext(context);
  10. }
  11. /*
  12. * Deprecated on API 23
  13. * Use onAttachToContext instead
  14. */
  15. @SuppressWarnings( “deprecation”)
  16. @Override
  17. public void onAttach(Activity activity) {
  18. super.onAttach(activity);
  19. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  20. onAttachToContext(activity);
  21. }
  22. }
  23. /*
  24. * Called when the fragment attaches to the context
  25. */
  26. protected void onAttachToContext(Context context) {
  27. //do something
  28. }


在用Fragment时,这样复写onAttach方法就可以减少API的影响了。ps:也真是出鬼了,昨天手机才升级的6.0,结果今天搞的手机测试正常,平板(api:4.4)测试不正常,愁死我了。这个问题告诉我,google不断发布兼容库意义重大啊!!!


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值