we can use intent to start activities, send broadcasts, start services, etc., in doing so, we can also add some additional data in the intent, has reached the effect of the value, such as the following code:
Mainactivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);intent.putExtra("name""xiaoming");intent.putExtra("age"17);startActivity(intent);
Secondactivity can get these values:
getIntent().getStringExtra("name");getIntent().getIntExtra("age"0);
This is just the use of intent to pass some values, how to pass the object?
There are two ways, one is serializable, the other is the parcelable way.
Serializable is the meaning of serialization, which means converting an object into a state that can be stored or transmitted. The serialized object can be transferred on the network or stored locally. The serialization method is very simple, just need to let a class to implement serializable this interface can be.
For example, a animal class, which contains the two fields of Type,sex, the code is as follows:
Public class Animal implements Serializable{ PrivateString type;PrivateString sex; PublicStringGetType() {returnType } Public void SetType(String type) { This. type = type; } PublicStirngGetsex() {returnSex } Public void Setsex(String Sex) { This. sex = sex; }}
Mainactivty.java
new Animal();animal.setType("panda");animal.setSex("male"new Intent(MainActivity.this, SecondActivity.class);intent,putExtra("animal_panda", animal);startActivity(intent);
Secondactivity.java
Animal animal = (Animal)getIntent().getSerializableExtra(animal_panda);
This calls the Getserializableextra () method to get the serialized object passed through the parameter, then transforms it down to person to be laid off, so that we have successfully implemented the function of using intent to pass the object.
In addition to serializable, using parcelable can achieve the same effect, but unlike serializing an object, the parcelable approach is to decompose a complete object, and each part of the decomposition is a data type supported by intent. , which implements the function of passing objects.
Public class Animal implements Serializable{ PrivateString type;PrivateString sex; PublicStringGetType() {returnType } Public void SetType(String type) { This. type = type; } PublicStirngGetsex() {returnSex } Public void Setsex(String Sex) { This. sex = sex; }@Override Public int describecontents() {return 0; }@Override Public void Writetoparcel(Parcel dest,intFlags) {dest.writestring (type); dest.writestring (Sex); } Public Static FinalParcelable.creator<animal> Creator =NewParcelable.creator<animal> () {@Override PublicAnimalCreatefromparcel(Parcel Source) {Animal Animal =NewAnimal (); Animal.type = Source.readstirng (); Animal.sex = Source.readstring ();returnAnimal }@Override PublicAniaml[]NewArray(intSize) {return NewAnimal[size]; } }}
Secondactivity.java
Animal animal = (Animal)getIntent.getParcelableExtra(animal-panda);
Existence is the reason, although the parcelable way is complex, but more efficient, it is generally recommended to use the parcelable way to achieve intent transfer function.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How to use intent to pass objects in Android