Android learning-detailed explanation of Android data transmission (serialization, parcelable, parcel, intent, bundle)

Source: Internet
Author: User
This article is from the network... For your reference, the data transmission methods between the first and fourth components are as follows: the four main components are started through the intent object. The intent function includes the four major components to be started and related information + data transmission. The data transmission intent provides the putextra and corresponding getextra methods for implementation: putextra and getextra actually correspond to bundle put and get methods one by one, in the intent class, all the putextra and getextra methods of the mexle's mextras member variables are actually used to call the put and get methods of the mextras object for access. Therefore, under normal circumstances, the data transmitted between the four components can be directly accessed through the putextra and getextra methods, without the need to create a bundle object. Intent putextra method: intent putextra (string name, bundle value)
Intent putextra (string
Name, parcelable [] value)
Intent putextra (string name, serializable
Value) intent putextra (string name, parcelable value)
Intent putextra (string
Name, int [] value)
Intent putextra (string name, float value)
Intent
Putextra (string name, byte [] value)
Intent putextra (string name, long []
Value)
Intent putextra (string name, float [] value)
Intent
Putextra (string name, long value)
Intent putextra (string name, string []
Value)
Intent putextra (string name, Boolean value)
Intent
Putextra (string name, Boolean [] value)
Intent putextra (string name, short
Value)
Intent putextra (string name, double value)
Intent putextra (string
Name, short [] value)
Intent putextra (string name, string value)
Intent
Putextra (string name, byte value)
Intent putextra (string name, char []
Value)
Intent putextra (string name, charsequence [] value)
Intent
Putextras (intent SRC)
Intent putextras (bundle extras)
Intent
Putintegerarraylistextra (string name, arraylist <integer> value)
Intent
Putparcelablearraylistextra (string name, arraylist <? Extends parcelable>
Value)
Intent putstringarraylistextra (string name, arraylist <string>
Value) intent getextra method: Double [] getdoublearrayextra (string name)
Double getdoubleextra (string
Name, double defaultvalue)
Bundle getextras ()
Int getflags ()
Float []
Getfloatarrayextra (string name)
Float getfloatextra (string name, float
Defaultvalue)
Int [] getintarrayextra (string name)
Int getintextra (string
Name, int defaultvalue)
Arraylist <integer>
Getintegerarraylistextra (string name)
Long [] getlongarrayextra (string
Name)
Long getlongextra (string name, long defaultvalue)
Parcelable []
Getparcelablearrayextra (string name)
<T extends parcelable>
Arraylist <t> getparcelablearraylistextra (string name)
<T extends
Parcelable> T getparcelableextra (string name)
Serializable
Getserializableextra (string name)
Short [] getshortarrayextra (string
Name)
Short getshortextra (string name, short defaultvalue)
String []
Getstringarrayextra (string name)
Arraylist <string>
Getstringarraylistextra (string name)
String getstringextra (string
Name) mextras object of intent: Private bundle mextras; public intent putextra (string name, parcelable value ){
If
(Mextras = NULL ){
Mexico tras = new bundle ();

}
Mexico tras. putparcelable (name, value );
Return this;

} Second, custom object transmission: There are two methods to pass custom objects through intent. The first is to implement the serialization interface, and the second is to implement the parcelable interface. Parceable is preferred. Many data types in Android are transmitted through the serialable interface, for example
Intent, bundle, bitmap, Uri, and so on. Android parcelable provides an interface parcelable and a tool class parcelparcelable: a standard interface that defines the features required to implement parcelable parcel: A Tool class for data access, it is used for users to access custom data when implementing the parcelable interface, and also for the system to transmit data. 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:
Writetoparcel (parcel DEST, int flags)
For more information about the parameters, 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) to create a class instance from in.
Newarray (INT size) creates an array of T type and size, with only one sentence (return New
T [size. It is estimated that this method is used for external class deserialization of this class array. Android Doc example Code :
    Public  Class  Myparcelable  Implements  Parcelable  {    Private  Int  Mdata  ;    Public  Int  Describecontents  ()  {   Return  0  ;    }    Public  Void  Writetoparcel  (  Parcel  Out  ,  Int  Flags  )  {    Out  . Writeint  (  Mdata  );    }    Public  Static  Final  Parcelable  .  Creator  <  Myparcelable  >  Creator  =  New  Parcelable .  Creator  <  Myparcelable  > ()  {    Public  Myparcelable  Createfromparcel  (  Parcel  In  )  {    Return  New  Myparcelable  ( In  );    }    Public  Myparcelable  []  Newarray  (  Int  Size  )  {    Return  New  Myparcelable  [  Size ];    }    };    Private  Myparcelable  (  Parcel  In  )  {  Mdata  =  In  .  Readint  ();   }     } 
 
 
In Android Custom object Serialization Problems Two options are parcelable and serializable.

I. serialization reason:

1. Permanently Save the object and save the object's byte sequence to a local file;
2. Passing objects in the network by serializing objects;
3. objects are passed between processes through serialization.

II
For which one to choose, refer to the following principles:

1. parcelable when using memory
Similar to serializable, parcelable is recommended.
2. serializable will generate a large number of temporary variables during serialization, resulting in frequent GC.
3. parcelable cannot be used when you want to store data on a disk, because parcelable cannot guarantee data continuity in the case of external changes. Although the serializable efficiency is low,
But in this case, we recommend that you use serializable.

Implementation:
1
Serializable implementation, only need to inherit implements serializable
You can. This only marks the object and the system will automatically serialize it.

2. To implement parcelabel, you must add a static member variable to the class.
Creator. This variable must inherit the parcelable. Creator interface.

 
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.