Android-Intent delivers data

I. briefly Intent can carry data. The common data that Intent can carry can be divided into the following categories: intent.putExtra(String name, boo...

I. briefly

Intent can carry data. The common data that Intent can carry can be divided into the following categories:

intent.putExtra(String name, boolean value); intent.putExtra(String name, byte value); intent.putExtra(String name, char value); intent.putExtra(String name, short value); intent.putExtra(String name, int value); intent.putExtra(String name, long value); intent.putExtra(String name, float value); intent.putExtra(String name, double value); intent.putExtra(String name, String value); intent.putExtra(String name, CharSequence value); intent.putExtra(String name, Parcelable value); intent.putExtra(String name, Parcelable[] value); intent.putExtra(String name, Serializable value); intent.putExtra(String name, boolean[] value); intent.putExtra(String name, byte[] value); intent.putExtra(String name, short[] value); intent.putExtra(String name, char[] value); intent.putExtra(String name, int[] value); intent.putExtra(String name, long[] value); intent.putExtra(String name, float[] value); intent.putExtra(String name, double[] value); intent.putExtra(String name, String[] value); intent.putExtra(String name, CharSequence[] value); intent.putExtra(String name, Bundle value);
The following four scenarios demonstrate Intent's transfer of data:

1) Basic data types

2) Transfer parameters by Bundle

3)serializable data type

4)parcelable data type

Among them, Serializable and Parcelabel should pay attention to:

1) Serializable implementation only needs to inherit implements Serializable. This is just marking the object and the system will serialize it automatically.
2) To implement Parcelabel, we need to add a static member variable CREATOR to the class, which needs to inherit the Parcelable.Creator interface.


Next, a demo will be used to demonstrate the transfer of various data types, and a second interface will be used to view the data in the form of a print log.

In the display result interface, Intent carries an int field "data_type" when it starts Activity to distinguish which type of data to display.

II. Test results

Look at the results of the print log first

1) Basic Type Data Output Log

02-21 09:03:19.569 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_boolean:true
02-21 09:03:19.569 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_byte:123
02-21 09:03:19.569 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_char:C
02-21 09:03:19.569 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_short:12
02-21 09:03:19.569 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_int:2
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_long:123456
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_float:1.23
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_double:3.1415926
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_String:string
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_CharSequence:charsequence
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_CharSequence_array:[ta, shi, what]
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_boolean_array:[true, false]
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_byte_array:[10, 1, 11]
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_short_array:[1, 2, 3, 4]
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_char_array:[A, B, C]
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_int_array:[1, 2, 3]
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_long_array:[4, 5, 6, 7, 8]
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_float_array:[1.1, 2.2, 3.3]
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_double_array:[4.4, 5.5, 6.6]
02-21 09:03:19.579 13666-13666/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBase: data_String_array:[string1, string2, string3]

The above output logs can be compared with the values transmitted by the code. The data received and transmitted are identical! ___________

2) Transfer data by Bundle

02-21 09:57:44.349 31882-31882/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBundle: bundle_int:1
02-21 09:57:44.349 31882-31882/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBundle: bundle_String:bundleString
02-21 09:57:44.349 31882-31882/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBundle: map_String:mapString
02-21 09:57:44.349 31882-31882/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBundle: map_Integer:2
02-21 09:57:44.349 31882-31882/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBundle: map_student id:18
02-21 09:57:44.349 31882-31882/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBundle: map_student name:wangWu
02-21 09:57:44.349 31882-31882/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBundle: map_student book author:luXun
02-21 09:57:44.349 31882-31882/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayBundle: map_student book name:yuWen

The output results are consistent with the parameters transferred. This demonstrates the transmission of basic data types and the serialization of data in a map.

3)serializable data type

02-21 09:14:48.709 22863-22863/com.mobile.cdtx.blog I/ReceiveParamsActivity: displaySerializable: student id:12
02-21 09:14:48.709 22863-22863/com.mobile.cdtx.blog I/ReceiveParamsActivity: displaySerializable: student name:zhangSan
02-21 09:14:48.709 22863-22863/com.mobile.cdtx.blog I/ReceiveParamsActivity: displaySerializable: student book author:yuguo
02-21 09:14:48.709 22863-22863/com.mobile.cdtx.blog I/ReceiveParamsActivity: displaySerializable: student book name:English

The output results are consistent with the parameters transferred. Demonstrates the case where a Student object contains a basic data type and another object, Book

4)parcelable data type

02-21 13:25:41.651 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable:age=15
02-21 13:25:41.651 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable:name=wwt
02-21 13:25:41.651 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable:gender=let
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_array:age=11
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_array:name=zh
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_array:gender=tt
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_array:age=13
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_array:name=tong
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_array:gender=ff
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type:age=18
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type:name=cup
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type:isOk=true
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type:strings=[123, 456, 789]
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type parcelableData:age=9
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type parcelableData:name=pear
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type parcelableData:gender=pink
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type ArrayList<ParcelableData>:age=45
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type ArrayList<ParcelableData>:name=pager
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type ArrayList<ParcelableData>:gender=red
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type ArrayList<ParcelableData>:age=78
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type ArrayList<ParcelableData>:name=pen
02-21 13:25:41.661 29743-29743/com.mobile.cdtx.blog I/ReceiveParamsActivity: displayParcelable: data_parcelable_type ArrayList<ParcelableData>:gender=blue

After testing, the output results are consistent with the transmitted data. Here we demonstrate the basic data type, transfer object, transfer object array and so on.

Three, code

1) main interface

There are four buttons on the main interface, which are used to test four kinds of situations.

The results are as follows:


activity_intent_params.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/activity_intent_params" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/id_btn_base" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Passing Basic Type Parameters"/> <Button android:id="@+id/id_btn_bundle" android:textAllCaps="false" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="transmit Bundle Type parameter"/> <Button android:id="@+id/id_btn_serializable" android:textAllCaps="false" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="transmit Serializable Type parameter"/> <Button android:id="@+id/id_btn_parcelable" android:textAllCaps="false" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="transmit Parcelable Type parameter"/> </LinearLayout>

Home Interface Code

import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.os.Parcelable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import com.mobile.cdtx.blog.R; import com.mobile.cdtx.blog.main.data.Book; import com.mobile.cdtx.blog.main.data.ParcelableData; import com.mobile.cdtx.blog.main.data.ParcelableType; import com.mobile.cdtx.blog.main.data.Student; import java.util.ArrayList; import java.util.HashMap; public class IntentParamsActivity extends AppCompatActivity implements View.OnClickListener{ public final static int DATA_BASE = 1; public final static int DATA_BUNDLE = 2; public final static int DATA_SERIALIZABLE =3; public final static int DATA_PARCELABLE = 4; Button btnBase,btnBundle,btnSerializable,btnParcelable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent_params); initView();//Initialization of controls } //Initialization of controls private void initView(){ btnBase = (Button)findViewById(R.id.id_btn_base); btnBundle = (Button)findViewById(R.id.id_btn_bundle); btnSerializable = (Button)findViewById(R.id.id_btn_serializable); btnParcelable = (Button)findViewById(R.id.id_btn_parcelable); btnBase.setOnClickListener(this); btnSerializable.setOnClickListener(this); btnParcelable.setOnClickListener(this); btnBundle.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.id_btn_base: baseParam(); break; case R.id.id_btn_bundle: bundleParam(); break; case R.id.id_btn_serializable: serializableParam(); break; case R.id.id_btn_parcelable: parcelableParam(); break; default: break; } } //Passing Basic Type Data private void baseParam(){ Intent intent = new Intent(IntentParamsActivity.this,ReceiveParamsActivity.class); intent.putExtra("data_type", DATA_BASE); //Basic data types intent.putExtra("data_boolean", new Boolean(true)); intent.putExtra("data_byte", new Byte("123")); intent.putExtra("data_char", new Character('C')); intent.putExtra("data_short", new Short("12")); intent.putExtra("data_int", new Integer(2)); intent.putExtra("data_long", new Long(123456)); intent.putExtra("data_float", new Float(1.23)); intent.putExtra("data_double", new Double(3.1415926)); intent.putExtra("data_String", new String("string")); CharSequence cs; Resources res=IntentParamsActivity.this.getResources(); cs=res.getText(R.string.charsequence); intent.putExtra("data_CharSequence", cs); String[] csArray={"ta","shi","what"}; intent.putExtra("data_CharSequence_array", csArray); boolean[] booleanArray = ; intent.putExtra("data_boolean_array", booleanArray); byte[] byteArray = ; intent.putExtra("data_byte_array", byteArray); short[] shortArray=; intent.putExtra("data_short_array", shortArray); char[] charArray = {'A','B','C'}; intent.putExtra("data_char_array",charArray); int[] intArray = ; intent.putExtra("data_int_array", intArray); long[] longArray = {(long)4,(long)5,(long)6,(long)7,(long)8}; intent.putExtra("data_long_array",longArray); float[] floatArray = {(float)1.1,(float)2.2, (float) 3.3}; intent.putExtra("data_float_array", floatArray); double[] doubleArray = ; intent.putExtra("data_double_array", doubleArray); String[] stringArray = {"string1","string2","string3"}; intent.putExtra("data_String_array", stringArray); startActivity(intent); } //Pass Bundle type data private void bundleParam(){ Intent intent = new Intent(IntentParamsActivity.this,ReceiveParamsActivity.class); intent.putExtra("data_type", DATA_BUNDLE); //Bundle can store all kinds of data and Intent can store data similar, here only show int,String, the actual use of Bundle more, such as: A - > B - > C, this situation is obviously more convenient to use Bundle, just pass it over once. Bundle bundle = new Bundle(); bundle.putInt("bundle_int",1); bundle.putString("bundle_String","bundleString"); //Passing HashMap data in Bundle: Note that this type of data can also be passed in Intent, as shown here only in Bundle HashMap hm=new HashMap(); hm.put("map_String","mapString"); hm.put("map_Integer",2); Student student = new Student(18,"wangWu",new Book("luXun","yuWen")); hm.put("map_student",student); bundle.putSerializable("bts_map", hm); intent.putExtra("data_Bundle", bundle); startActivity(intent); } //Pass data of type serializable private void serializableParam(){ Intent intent = new Intent(IntentParamsActivity.this,ReceiveParamsActivity.class); intent.putExtra("data_type", DATA_SERIALIZABLE); Student student = new Student(12,"zhangSan",new Book("yuguo","English")); intent.putExtra("data_Serializable", student); startActivity(intent); } //Passing data of type parcelable private void parcelableParam(){ Intent intent = new Intent(IntentParamsActivity.this,ReceiveParamsActivity.class); intent.putExtra("data_type", DATA_PARCELABLE); //Pass an object data ParcelableData parcelableData1 = new ParcelableData(); parcelableData1.setAge(15); parcelableData1.setName("wwt"); parcelableData1.setGender("let"); intent.putExtra("data_parcelable", parcelableData1); //Pass an object array data ParcelableData parcelableData2 = new ParcelableData(); parcelableData2.setAge(11); parcelableData2.setName("zh"); parcelableData2.setGender("tt"); ParcelableData parcelableData3 = new ParcelableData(); parcelableData3.setAge(13); parcelableData3.setName("tong"); parcelableData3.setGender("ff"); ParcelableData[] parcelableDatas = ; intent.putExtra("data_parcelable_array",(Parcelable[])parcelableDatas); //Transfer a relatively complex data ParcelableType parcelableType = new ParcelableType(); parcelableType.setAge(18); parcelableType.setName("cup"); parcelableType.setOk(true); parcelableType.setStrings(new String[]{"123","456","789"}); ParcelableData parcelableData4 = new ParcelableData(); parcelableData4.setAge(9); parcelableData4.setName("pear"); parcelableData4.setGender("pink"); parcelableType.setParcelableData(parcelableData4); ArrayList<ParcelableData> listParcelableData = new ArrayList<ParcelableData>(); ParcelableData parcelableData5 = new ParcelableData(); parcelableData5.setAge(45); parcelableData5.setName("pager"); parcelableData5.setGender("red"); ParcelableData parcelableData6 = new ParcelableData(); parcelableData6.setAge(78); parcelableData6.setName("pen"); parcelableData6.setGender("blue"); listParcelableData.add(parcelableData5); listParcelableData.add(parcelableData6); parcelableType.setListParcelableData(listParcelableData); intent.putExtra("data_parcelable_type", parcelableType); startActivity(intent); } }

2) Display result interface

The results are as follows:


Layout file activity_receive_params.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/activity_receive_params" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/id_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="View passed parameters by printing logs"/> <Button android:id="@+id/id_btn_finish" android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Return"/> </LinearLayout>
Home Interface Code

import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import com.mobile.cdtx.blog.R; import com.mobile.cdtx.blog.main.data.ParcelableData; import com.mobile.cdtx.blog.main.data.ParcelableType; import com.mobile.cdtx.blog.main.data.Student; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; public class ReceiveParamsActivity extends AppCompatActivity { private Button btnFinish; private static final String TAG = "ReceiveParamsActivity"; public final static int DATA_BASE = 1; public final static int DATA_BUNDLE = 2; public final static int DATA_SERIALIZABLE =3; public final static int DATA_PARCELABLE = 4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_receive_params); btnFinish = (Button)findViewById(R.id.id_btn_finish); btnFinish.setOnClickListener(new View. OnClickListener(){ @Override public void onClick(View v) { finish(); } }); //Analytical data Intent intent = getIntent(); int dataType = intent.getIntExtra("data_type",0); displayData(dataType,intent); } //Show different types of data private void displayData(int dataType,Intent intent){ switch (dataType){ case DATA_BASE: displayBase(intent); break; case DATA_BUNDLE: displayBundle(intent); break; case DATA_SERIALIZABLE: displaySerializable(intent); break; case DATA_PARCELABLE: displayParcelable(intent); break; default: break; } } //Show basic types of data private void displayBase(Intent intent){ //Parsing basic data types boolean booleanData = intent.getBooleanExtra("data_boolean",true); byte byteData = intent.getByteExtra("data_byte", (byte)0); char characterData = intent.getCharExtra("data_char",'Z'); short shortData = intent.getShortExtra("data_short",(short)1); int intData = intent.getIntExtra("data_int",2); long longData = intent.getLongExtra("data_long",(long)1); float floatData = intent.getFloatExtra("data_float",(float)1.0); double doubleData = intent.getDoubleExtra("data_double",2.0); String stringData = intent.getStringExtra("data_String"); CharSequence cs = intent.getCharSequenceExtra("data_CharSequence"); //Parsing arrays of basic data types CharSequence[] stringArray = intent.getCharSequenceArrayExtra("data_CharSequence_array"); boolean[] booleanArray = intent.getBooleanArrayExtra("data_boolean_array"); byte[] byteArray = intent.getByteArrayExtra("data_byte_array"); short[] shortsArray = intent.getShortArrayExtra("data_short_array"); char[] charArray = intent.getCharArrayExtra("data_char_array"); int[] intArray = intent.getIntArrayExtra("data_int_array"); long[] longArray = intent.getLongArrayExtra("data_long_array"); float[] floatArray = intent.getFloatArrayExtra("data_float_array"); double[] doubleArray = intent.getDoubleArrayExtra("data_double_array"); String[] stringsArray = intent.getStringArrayExtra("data_String_array"); //Print Basic Type Logs Log.i(TAG, "displayBase: data_boolean:"+String.valueOf(booleanData)); Log.i(TAG, "displayBase: data_byte:"+String.valueOf(byteData)); Log.i(TAG, "displayBase: data_char:"+String.valueOf(characterData)); Log.i(TAG, "displayBase: data_short:"+String.valueOf(shortData)); Log.i(TAG, "displayBase: data_int:"+String.valueOf(intData)); Log.i(TAG, "displayBase: data_long:"+String.valueOf(longData)); Log.i(TAG, "displayBase: data_float:"+String.valueOf(floatData)); Log.i(TAG, "displayBase: data_double:"+String.valueOf(doubleData)); Log.i(TAG, "displayBase: data_String:"+stringData); Log.i(TAG, "displayBase: data_CharSequence:"+String.valueOf(cs)); //Print basic type array logs Log.i(TAG, "displayBase: data_CharSequence_array:"+ Arrays.toString(stringArray)); Log.i(TAG, "displayBase: data_boolean_array:"+ Arrays.toString(booleanArray)); Log.i(TAG, "displayBase: data_byte_array:"+ Arrays.toString(byteArray)); Log.i(TAG, "displayBase: data_short_array:"+ Arrays.toString(shortsArray)); Log.i(TAG, "displayBase: data_char_array:"+ Arrays.toString(charArray)); Log.i(TAG, "displayBase: data_int_array:"+ Arrays.toString(intArray)); Log.i(TAG, "displayBase: data_long_array:"+ Arrays.toString(longArray)); Log.i(TAG, "displayBase: data_float_array:"+ Arrays.toString(floatArray)); Log.i(TAG, "displayBase: data_double_array:"+ Arrays.toString(doubleArray)); Log.i(TAG, "displayBase: data_String_array:"+ Arrays.toString(stringsArray)); } //Show Bundle type data private void displayBundle(Intent intent){ Bundle bundle = intent.getBundleExtra("data_Bundle"); int intBundle = bundle.getInt("bundle_int"); String stringBundle = bundle.getString("bundle_String"); //Getting ap data HashMap map=(HashMap)bundle.getSerializable("bts_map"); String mapString = (String) map.get("map_String"); int mapInteger = (int)map.get("map_Integer"); Student mapStudent = (Student)map.get("map_student"); //Print basic data in Bundle Log.i(TAG, "displayBundle: bundle_int:"+String.valueOf(intBundle)); Log.i(TAG, "displayBundle: bundle_String:"+String.valueOf(stringBundle)); //Print map data in Bundle Log.i(TAG, "displayBundle: map_String:"+mapString); Log.i(TAG, "displayBundle: map_Integer:"+String.valueOf(mapInteger)); Log.i(TAG, "displayBundle: map_student id:"+mapStudent.getId()); Log.i(TAG, "displayBundle: map_student name:"+mapStudent.getName()); Log.i(TAG, "displayBundle: map_student book author:"+mapStudent.getBook().getAuthor()); Log.i(TAG, "displayBundle: map_student book name:"+mapStudent.getBook().getName()); } //Display data of serializable type private void displaySerializable(Intent intent){ Student student = (Student) intent.getSerializableExtra("data_Serializable"); //Print Student Log Log.i(TAG, "displaySerializable: student id:"+student.getId()); Log.i(TAG, "displaySerializable: student name:"+student.getName()); Log.i(TAG, "displaySerializable: student book author:"+student.getBook().getAuthor()); Log.i(TAG, "displaySerializable: student book name:"+student.getBook().getName()); } //Display data of parcelable type private void displayParcelable(Intent intent){ //Get the transferred data ParcelableData parcelableData = intent.getParcelableExtra("data_parcelable"); Parcelable[] ParcelableDataArray = intent.getParcelableArrayExtra("data_parcelable_array"); ParcelableType parcelableType = intent.getParcelableExtra("data_parcelable_type"); //Print logs separately //Print data_parcelable Log.i(TAG, "displayParcelable: data_parcelable:age="+parcelableData.getAge()); Log.i(TAG, "displayParcelable: data_parcelable:name="+parcelableData.getName()); Log.i(TAG, "displayParcelable: data_parcelable:gender="+parcelableData.getGender()); //Print data_parcelable_array if(ParcelableDataArray != null && ParcelableDataArray.length > 0){ for (Parcelable data:ParcelableDataArray){ Log.i(TAG, "displayParcelable: data_parcelable_array:age="+((ParcelableData)data).getAge()); Log.i(TAG, "displayParcelable: data_parcelable_array:name="+((ParcelableData)data).getName()); Log.i(TAG, "displayParcelable: data_parcelable_array:gender="+((ParcelableData)data).getGender()); } } // // Print data_parcelable_type Log.i(TAG, "displayParcelable: data_parcelable_type:age="+parcelableType.getAge()); Log.i(TAG, "displayParcelable: data_parcelable_type:name="+parcelableType.getName()); Log.i(TAG, "displayParcelable: data_parcelable_type:isOk="+parcelableType.isOk()); Log.i(TAG, "displayParcelable: data_parcelable_type:strings="+Arrays.toString(parcelableType.getStrings())); //Print objects in data_parcelable_type Log.i(TAG, "displayParcelable: data_parcelable_type parcelableData:age="+parcelableType.getParcelableData().getAge()); Log.i(TAG, "displayParcelable: data_parcelable_type parcelableData:name="+parcelableType.getParcelableData().getName()); Log.i(TAG, "displayParcelable: data_parcelable_type parcelableData:gender="+parcelableType.getParcelableData().getGender()); //Print arrays in data_parcelable_type ArrayList<ParcelableData> ParcelableDataArray2 = parcelableType.getListParcelableData(); for(ParcelableData parcelableData2 : ParcelableDataArray2){ Log.i(TAG, "displayParcelable: data_parcelable_type ArrayList<ParcelableData>:age="+parcelableData2.getAge()); Log.i(TAG, "displayParcelable: data_parcelable_type ArrayList<ParcelableData>:name="+parcelableData2.getName()); Log.i(TAG, "displayParcelable: data_parcelable_type ArrayList<ParcelableData>:gender="+parcelableData2.getGender()); } } }

Four. Conclusion

1. When using memory, the Parcelable class has better performance than Serializable, so it is recommended to use the Parcelable class.
2.Serializable produces a large number of temporary variables during serialization, which results in frequent GC.
3.Parcelable can't be used when data is stored on disk, because Parcelable can't guarantee the persistence of data in the case of external changes. Although Serializable is inefficient and not advocated, in this case, it is recommended that you use Serializable.

31 March 2019, 23:33 | Views: 9432

Add new comment

For adding a comment, please log in
or create account

0 comments