By implementing the Serializable interface
Using the features of the Java language itself, it is passed through the serialization of the data.
Entity class:
public class Person implements Serializable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
1, set the parameters
//1. Example of passing parameters via the Serializable interface
//HashMap<String,String> map2 = new HashMap<>();
//map2.put("key1", "value1");
//map2.put("key2", "value2");
//Bundle bundleSerializable = new Bundle();
//bundleSerializable.putSerializable("serializable", map2);
//Intent intentSerializable = new Intent();
//intentSerializable.putExtras(bundleSerializable);
//intentSerializable.setClass(MainActivity.this,
//SerializableActivity.class);
//startActivity(intentSerializable);
//2. Passing the entity class through the Serializable interface
Person person = new Person();
person.setAge(25);
person.setName("lyx");
Intent intent2 = new Intent(MainActivity.this, SerializableActivity.class);
intent2.putExtra("serializable", person);
startActivity(intent2);
2, receiving parameters
this.setTitle("Serializable example");
/ / Receive parameters
//1. Receive collection
// Bundle bundle = this.getIntent().getExtras();
/ / If LinkedHashMap, then bundle.getSerializable will report ClassCastException, do not know why
// There is no problem with passing HashMap.
//HashMap<String, String> map = (HashMap<String, String>) bundle.getSerializable("serializable");
//
//String sResult = "map.size() =" + map.size();
//
//Iterator iter = map.entrySet().iterator();
// while (iter.hasNext()) {
// Map.Entry entry = (Map.Entry) iter.next();
// Object key = entry.getKey();
// Object value = entry.getValue();
// //System.out.println("key---->"+ (String)key);
// //System.out.println("value---->"+ (String)value);
//
// sResult += "\r\n key----> " + (String) key;
// sResult += "\r\n value----> " + (String) value;
// }
//2. Receive entity class
Person person = (Person) getIntent().getSerializableExtra("serializable");
String sResult = "Name:" + person.getName() + "--age:" + person.getAge();
TextView tv = findViewById(R.id.tv);
tv.setText(sResult);
Pass data to the next activity Using the putExtra() method in the Intent object, we can temporarily store the data we want to deliver in the Intent. After starting another activity, we only need to ta...
Need to transfer data in different Activity, you can attach the data when the original Activity uses Intent to jump to the new Activity, and then you can receive it in the new Activity. Let's take the...
Implementing the serialization interface Serializable can also control the serialization control of variables and transient variables, see the code The result of the operation is It can be found that ...
purpose The purpose of the class to implement the serializable interface is mainly to perform a persistence operation, and the data of the temporary stored in the memory block is converted into a tran...
The Serializable interface is a markup interface. Because it has no methods, there is no need to add extra code to the class to implement the Serializable interface. Implementing this interface starts...
Introduction to immersive status bar Immersive status bar style Android 4.4 introduces a mechanism that can integrate the color of the status bar at the top of the phone into the interface of the curr...
I want to implement this feature today, but online search code is used to implement the ways such as setPadding, setmargin, which is no problem before Android 4.0, but the system has provided us with ...
The Serializable interface is a serialization interface provided by Java. It is an empty interface that provides standard serialization and deserialization operations for objects. Using Serializable i...
categories: JavaWeb A good hierarchical division can not only make the code structure clearer, but also make the project division more clear, and the readability is greatly improved, which is more con...
Article catalog 1, mode one (realize the serializable interface, through serialization) 2, mode 2 (implement the externalizable interface, rewriting WriteExternal and READEXTERNAL methods) Object sequ...