归志宁无五亩园,读书本意在元元。这篇文章主要讲述Android中数据的传递相关的知识,希望能为你提供帮助。
android中当两个Activity需要有信息交互的时候,可以使用Intent。具体来说:
发送单一类型数据:
发送方: String data = "https://www.songbingjia.com/android/Hello SecondActivity"; Intent intent = new Intent(FirstActivity.this, SecondActivity.class); intent.putExtra("extra_data", data); startActivity(intent); #在onClick中调用 接收方: Intentintent = getIntent(); String data = https://www.songbingjia.com/android/intent.getStringExtra("extra_data"); #可以在onCreate中读取
除了putExtra("key", value)/getStringExtra("key")之外, 根据传递的value的类型还有很多对,比如:
putExtra("key", intvalue)/getintExtra("key") putExtra("key", floatvalue)/getfloatExtra("key") putExtra("key", doublevalue)/getDoubleExtra("key")
putExtra("key", bundlevalue)/getBundleExtra("key")
...
发送多种数据类型的组合:
也可以用Bundle把很多数据类型(包括嵌套Bundle)放到Bundle中一并发送:
//sender
Bundle b = new Bundle(); b.putInt("age", 2); b.putString("data", "hello"); intent.putExtras(b); startActivity(intent);
//receiver
Intent i = getIntent(); Bundle b = i.getExtras(); int age = b.getInt("age"); String data = https://www.songbingjia.com/android/b.getString("data");
发送类对象:
方法1:Serializable
Serializable是java提供的序列化方法,优点是使用简单,一切由系统负责;缺点是速度稍慢。
public class User implements Serializable{ private String name; private Integer age; public String getName() { return name; }public void setName(String name) { this.name = name; }public Integer getAge() { return age; }public void setAge(Integer age) { this.age = age; }public User(String name, Integer age){ this.name = name; this.age = age; } }
//sender
intent.putExtra("user", (Serializable) new User("JM", 30));
//Receiver Intent i = getIntent(); User user = (User) i.getSerializableExtra("user"); textView.setText(String.format("name: %s, age: %s", user.getName(), user.getAge()));
方法2:Parcelable
public class User implements Parcelable{ private String name; private Integer age; public String getName() { return name; }public void setName(String name) { this.name = name; }public Integer getAge() { return age; }public void setAge(Integer age) { this.age = age; }public User(String name, Integer age){ this.name = name; this.age = age; }@Override public int describeContents() { return 0; }@Override public void writeToParcel(Parcel parcel, int i) { //write all the fields that want to send parcel.writeString(getName()); parcel.writeInt(getAge()); }public static final Creator< User> CREATOR = new Creator< User> () { @Override public User createFromParcel(Parcel parcel) { return new User(parcel.readString(), parcel.readInt()); }@Override public User[] newArray(int i) { return new User[i]; } }; }
//Sender intent.putExtra("user", (Parcelable) new User("JM", 30)); startActivity(intent);
//Receiver User user = (User) i.getParcelableExtra("user"); textView.setText(String.format("name: %s, age: %s", user.getName(), user.getAge()));
如果在User中有多个同类型字段,比如String,可以用Bundle来实现:
@Override public void writeToParcel(Parcel parcel, int i) { //write all the fields that want to send //parcel.writeString(getName()); //parcel.writeInt(getAge()); Bundle b = new Bundle(); b.putString("name", getName()); b.putInt("age", getAge()); parcel.writeBundle(b); }public static final Creator< User> CREATOR = new Creator< User> () { @Override public User createFromParcel(Parcel parcel) { //return new User(parcel.readString(), parcel.readInt()); Bundle b = parcel.readBundle(); return new User(b.getString("name"), b.getInt("age")); }@Override public User[] newArray(int i) { return new User[i]; } };
从别的Activity得到返回结果:
启动端:
startActivityForResult(intent, 0);
0表示请求码,说明此次请求的意图是什么。
接收端:
Intent i = new Intent(); i.putExtra("data", editText.getText().toString()); setResult(RESULT_OK, i); finish();
【Android中数据的传递】
推荐阅读
- Android软件开发之SharedPreferences
- android应用内存占用测试(每隔一秒打印procrank的信息)
- android studio中常用快捷键
- pod install和pod update – CocoaPods教程
- 安装和使用CocoaPods – CocoaPods教程
- 准备行为面试问题的6个技巧
- 2020年学习TypeScript的5个理由
- Hackathon团队需要具备的5种必备技能
- Facebook使用机器学习的5种令人振奋的方式