Android provides a new type: parcel. This class is used as a container to encapsulate data. The encapsulated data can be transmitted through intent or IPC. Except for the basic type
Only the classes that implement the parcelable interface can be put into parcel.
Parcelable implementation key points: three things need to be implemented
1) writetoparcel method. This method writes the class data to the external provided parcel. The declaration is as follows:
For details about writetoparcel (parcel DEST, int flags), see javadoc.
2) describecontents method. I don't know what it is for. It's okay to return 0 directly.
3) Static parcelable. Creator interface. This interface has two methods:
Createfromparcel (parcel in) is used to create a class instance from in.
Newarray (INT size) creates an array of T type and size. It can only be a single sentence (return New T [size. It is estimated that this method is used for external class deserialization of this class array.
Receive information activity for testing
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = getIntent();
Person p = i.getParcelableExtra("yes");
System.out.println("---->"+p.name);
System.out.println("---->"+p.map.size());
}
}
Activity sent
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TestNew extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
Person p = new Person();
p.map = new HashMap<String,String>();
p.map.put("yes", "ido");
p.name="ok";
intent.putExtra("yes", p);
intent.setClass(this, Test.class);
startActivity(intent);
}
}
Implementation class of parcelable
Import java. util. hashmap;
Import Android. OS. parcel;
Import Android. OS. parcelable;
Public class person implements parcelable {
Public hashmap <string, string> map = new hashmap <string, string> ();
Public string name;
@ Override
Public int describecontents (){
Return 0;
}
@ Override
Public void writetoparcel (parcel DEST, int flags ){
DeST. writemap (MAP );
DeST. writestring (name );
}
Public static final parcelable. creator <person> creator = new parcelable. creator <person> (){
// Rewrite the Creator
@ Override
Public Person createfromparcel (parcel source ){
Person P = new person ();
P. Map = source. readhashmap (hashmap. Class. getclassloader ());
P. Name = source. readstring ();
Return P;
}
@ Override
Public person [] newarray (INT size ){
// Todo auto-generated method stub
Return NULL;
}
};
}
Below I will repost it from Ghost:
In the SDK, The parcelable class is described as follows: interface for classes whose instances can be written to and restored fromParcel
. Classes implementing the parcelable interface must also have a static field calledCREATOR
, Which is an object implementingParcelable.Creator
Interface.
Instances of this interface are stored through parcel. parcelable. creator must be used when parcelable is used.
The code below:
First, modify main. xml and add a button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parcelable">
</Button>
</LinearLayout>
Next, the main activity parcelabletest. java. This class displays the UI and uses the button to click the event to start another activity-parcelabletest2. At the same time, some data is transmitted through the parcelable interface.
package parcelable_test.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ParcelableTest extends Activity implements OnClickListener{
public static final String KEY = "key";
private Button button;
public static final String TAG = "Parcelable";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
Log.d(TAG, "ParcelableTest");
}
private void init(){
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
private void fun(){
Log.d(TAG, "fun");
Person mPerson = new Person();
mPerson.setName("tom");
mPerson.setAge(25);
Intent mIntent = new Intent(this,parcelable_test.com.ParcelableTest2.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(KEY, mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
fun();
break;
default:
break;
}
}
}
Parcelabletest2.java, which is used to obtain data transmitted from parcelabletest and display it on the UI.
package parcelable_test.com;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ParcelableTest2 extends Activity{
private static final String TAG = ParcelableTest.TAG;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "ParcelableTest2");
TextView textView = new TextView(this);
Person mPerson = (Person)getIntent().getParcelableExtra(ParcelableTest.KEY);
textView.setText("name = " + mPerson.getName() + " age = " + mPerson.getAge());
setContentView(textView);
}
}
The following is the most important class person. The person class references the parcelable interface.
package parcelable_test.com;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class Person implements Parcelable{
private String name;
private int age;
private static final String TAG = ParcelableTest.TAG;
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;
}
public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
Log.d(TAG,"createFromParcel");
Person mPerson = new Person();
mPerson.name = source.readString();
mPerson.age = source.readInt();
return mPerson;
}
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return new Person[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
Log.d(TAG,"describeContents");
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
Log.d(TAG,"writeToParcel");
dest.writeString(name);
dest.writeInt(age);
}
}
Log shows the program running status in mbundle. putparcelable (Key, mperson);, calls the public void writetoparcel (parcel DEST, int flags) method in the person class, and writes data to the dest, at person mperson = (person) getintent (). getparcelableextra (parcelabletest. the public person createfromparcel (parcel source) method in the person class is called, a person object is created, and the attributes of the object are assigned a value, here, the parcel source and parcel DEST are the same, and the person object is returned. Finally, you can print the attributes of mperson.
Parcel can be used to store a lot of data, such as string and INT, or a set of string and hashmap <string, string>.