关于Android Activity之间传递数据的6种方式
更新时间:2017年03月20日 09:51:56 作者:呆萌先生
我要评论
这篇文章主要介绍了关于Android Activity之间传递数据的6种方式,非常不错,具有参考借鉴价值,需要的朋友可以参考下

使用Inten的putExtra传递
第一个Activity中
?
1 2 3 4 5 6 | //创建意图对象 Intent intent = new Intent( this ,TwoActivity. class ); //设置传递键值对 intent.putExtra( "data" ,str); //激活意图 startActivity(intent); |
第二个Activity中
?
1 2 3 4 5 6 | // 获取意图对象 Intent intent = getIntent(); //获取传递的值 String str = intent.getStringExtra( "data" ); //设置值 tv.setText(str); |
使用Intention的Bundle传递
第一个Activity中
?
1 2 3 4 5 6 7 8 9 | //创建意图对象 Intent intent = new Intent(MainActivity. this ,TwoActivity. class ); //用数据捆传递数据 Bundle bundle = new Bundle(); bundle.putString( "data" , str); //把数据捆设置改意图 intent.putExtra( "bun" , bundle); //激活意图 startActivity(intent); |
第二个Activity
?
1 2 3 4 5 | //获取Bundle Intent intent = getIntent(); Bundle bundle = intent.getBundleExtra( "bun" ); String str = bundle.getString( "data" ); tv.setText(str); |
使用Activity销毁时传递数据
第一个Activity中
?
1 2 3 4 5 6 7 8 9 10 | Intent intent = new Intent(MainActivity. this ,TwoActivity. class ); //用一种特殊方式开启Activity startActivityForResult(intent, 11 ); //设置数据 protected void onActivityResult( int requestCode, int resultCode, Intent data) { super .onActivityResult(requestCode, resultCode, data); String str = data.getStringExtra( "data" ); tvOne.setText(str); } |
第二个activity中
?
1 2 3 4 5 6 | //设置返回的数据 Intent intent = new Intent(); intent.putExtra( "data" , edtOne.getText().toString().trim()); setResult( 3 , intent); //关闭当前activity finish(); |
SharedPreferences传递数据
第一个Activity中
?
1 2 3 4 5 6 7 8 9 | SharedPreferences sp = this .getSharedPreferences( "info" , 1 ); //获取sp编辑器 Editor edit = sp.edit(); edit.putString( "data" , str); edit.commit(); //创建意图对象 Intent intent = new Intent(MainActivity. this ,TwoActivity. class ); //激活意图 startActivity(intent); |
第二个Activity中
?
1 2 3 | SharedPreferences sp = this .getSharedPreferences( "info" , 1 ); //设置数据 tv.setText(sp.getString( "data" , "" )); |
使用序列化对象Seriazable
工具类
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.io.Serializable; class DataBean implements Serializable { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this .name = name; } public String getSex() { return sex; } public void setSex(String sex) { this .sex = sex; } } |
第一个Activity
?
1 2 3 4 5 6 7 8 | //创建意图 Intent intent = new Intent(MainActivity. this ,TwoActivity. class ); DataBean bean = new DataBean(); //通过set方法把数据保存到DataBean对象中 bean.setName( "啦啦" ); bean.setSex( "男" ); intent.putExtra( "key" , bean); startActivity(intent); |
第二个Activity
?
1 2 3 4 5 6 7 8 | Intent intent = getIntent(); //反序列化数据对象 Serializable se = intent.getSerializableExtra( "key" ); if (se instanceof DataBean){ //获取到携带数据的DataBean对象db DataBean db = (DataBean) se; tv.setText(db.getName()+ "===" +db.getSex()); } |
使用静态变量传递数据
第一个Activity
?
1 2 3 4 | Intent intent = new Intent(MainActivity. this ,TwoActivity. class ); TwoActivity.name= "牛逼" ; TwoActivity.str= "你说" ; startActivity(intent); |
第二个Activity
?
1 2 3 4 | //静态变量 protected static String name; protected static String str; tv.setText(str+name); |
以上所述是小编给大家介绍的关于Android Activity之间传递数据的6种方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
原文链接:http://blog.youkuaiyun.com/good__man/article/details/63812419