自学安卓复习基础_之四(关于intent和bundle传值)

来源:互联网 发布:leetcode面试题js 编辑:程序博客网 时间:2024/05/20 23:58

一开始学习传值的时候我有些疑惑,bundle可以传值,intent也可以传值,那为什么有时候用bundle,有时候用intent呢,而有时候又两者结合在一起使用呢?后面根据自己编码习惯,觉得差不多(我还是初级菜鸟所以理解肯定还不透彻,希望得到高人指点)今天我就写写他们的基本实现方式:
一:intent传值

    //在活动A中传递数据    Intent intent =new Intent(……);    intent.putExtra("key",value);    startActivity(intent);    //在活动B中接收数据    Intent getIntent=getIntent();    getIntent.getStringExtra("key");

二:bundle传值

    //在活动A中传递数据    Bundle bundle=new Bundle();    bundle.putString("key",value);    //在活动B中接收数据    Bundle getBundle=getArguments();    getBundle.getString("key");

三:Intent结合Bundle传数据

    //在活动A中传递数据    Intent intent=new Intent(……);    Bundle bundle=new Bundle();    bundle.putString("key",value);    intent.putExtras(bundle);    startActivity(intent);    //在活动B中得到数据    Bundle bundle=getIntent().getExtras();    String value=bundle.getString("key");
0 0
原创粉丝点击