today In two Activity Transfer set type data between , Look at the , Use Parcelable So take a look at something :

The following paragraph is a copy of netizens .

Android There are two main ways to serialize objects , Realization Serializable Interface 、 Or realize Parcelable Interface . Realization Serializable Interface is Java SE It's supported by itself , and Parcelable yes Android Unique function , Efficiency is better than achievement Serializable High interface , And it can also be used in IPC in . Realization Serializable The interface is very simple , Just make a statement , To achieve Parcelable The interface is a little more complicated , But it's more efficient , This method is recommended to improve performance . Let's talk about the implementation Parcelable Interface method  
        By implementing Parcelable Interface to serialize objects : 
        1、 Declare the implementation interface Parcelable 
        2、 Realization Parcelable Methods writeToParcel, Serialize your object into a Parcel object  
        3、 Instantiate static internal objects CREATOR Implementation interface Parcelable.Creator:

package com.hct.demo;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log; /**
*
* @time 2013-11-26 Afternoon 4:12:46
* @todo person Entity class , Realization Parcelable Interface class
*/ public class Person implements Parcelable {
private static final String TAG = ParcelableTest.TAG; private int age;
private String name; public Person(){} public Person(String name, int age) {
super();
this.name = name;
this.age = 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;
} /**
* 3、 Instantiate static internal objects CREATOR Implementation interface Parcelable.Creator:Java Code Collection code
public static final Parcelable.Creator<T> CREATOR among public static final Not one less
, Internal object CREATOR You can't change the name of , It has to be all in capitals .
4、 complete CREATOR Code for , Implementation method createFromParcel, take Parcel Object is deserialized to your object In short : adopt writeToParcel Map your object to Parcel object , Re pass createFromParcel take Parcel Objects map to your objects
. Can also be Parcel See it as a stream , adopt writeToParcel Write objects into the stream , Through createFromParcel Reading objects from the stream
, It's just that this process needs you to do , So the order of writing and the order of reading must be the same .
*/
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) {
return new Person[size];
}
}; @Override
public int describeContents() {
Log.d(TAG, "describeContents");
return 0;
} /**
* Realization Parcelable Methods writeToParcel, Serialize your object into a Parcel object
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
Log.d(TAG, "writeToParcel");
dest.writeString(name);
dest.writeInt(age);
}
}
package com.hct.demo;

import java.util.ArrayList;

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,ParcelableTest2.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(KEY, mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
} private void funList(){
ArrayList<Person> list = new ArrayList<Person>();
for(int i = 0;i<5;i++){
Person mPerson = new Person("abc"+i,i*5);
list.add(mPerson);
}
Intent mIntent = new Intent(this,ParcelableTest2.class);
mIntent.putParcelableArrayListExtra(KEY, list);
startActivity(mIntent);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
funList();
break; default:
break;
}
}
}
package com.hct.demo;

import java.util.ArrayList;

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); ArrayList<Person> arrays = getIntent().getParcelableArrayListExtra(ParcelableTest.KEY);
StringBuilder sb = new StringBuilder();
for(int i=0;i<arrays.size();i++){
Person mPerson = arrays.get(i);
sb.append("name = " + mPerson.getName() + " age = " + mPerson.getAge()).append("\n");
} textView.setText(sb.toString()); setContentView(textView);
}
}

It turns out

Android Use in Parcelable More articles about

  1. Android Medium Parcelable Interface and Serializable Usage and difference

    Parcelable Interface : Interface for classes whose instances can be written to and restored from a Parcel. Cl ...

  2. android To realize Parcelable Serialization steps

    import java.io.Serializable; import java.text.DecimalFormat; import android.os.Parcel; import androi ...

  3. Android How to use Intent stay Activity Passing objects between [ Use Serializable perhaps Parcelable]

    http://blog.csdn.net/cjjky/article/details/6441104 stay Android Different in Activity Passing objects between , We can consider adopting Bundle.putSeri ...

  4. Android Medium AIDL,Parcelable And remote services

    Android Medium AIDL,Parcelable And remote services       I came into contact with during my early study AIDL, At that time, the use of this is also a skim . It's only recently that I came into contact with AIDL, It's just beginning to go deep .AIDL The role of     ...

  5. Android in Intent Two ways to pass objects (Serializable,Parcelable)

    Today I want to talk about Android in Intent How to transfer objects in , As far as I know, there are two ways , One is Bundle.putSerializable(Key,Object); The other is Bundle.putP ...

  6. 【 turn 】Android How to use Bundle Transfer object [ Use Serializable perhaps Parcelable] -- Pretty good

    Original website :http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1211/694.html Android in Bundle The role of classes Bun ...

  7. Android in Parcelable Serialization summary

    In the use of Parcelable Yes android The serialization operation of data in is more useful , Someone has done it by comparison Serializable and Parcelable stay android Speed comparison of serialization operation objects in , Probably Parcela ...

  8. Android in Parcelable Interface

    1. Parcelable Interface Interface for classes whose instances can be written to and restored from a Parcel. ...

  9. Talking about Android in Serializable and Parcelable Use difference

    Copyright notice : This article is from Wang Lei's blog , Please indicate the source of reprint . One . summary Android When developing , We have a need to deliver objects for a long time , But we can't pass a reference to an object to Activity perhaps Fragment, We need to put these objects ...

Random recommendation

  1. 【 Front end accumulation 】JavascriptUtil String part

    // Calculate string length String.prototype.strLen = function() { var len = 0; for (var i = 0; i < this.length; i+ ...

  2. Xinde `Context` class

    Context, Translated into Chinese “ Context ”,SDK The explanation is as follows : Interface to global information about an application environment. Thi ...

  3. socket.io+angular.js+express.js Make a chat app ( Four )

    Next to the last one Use angularjs Building chat rooms client <!doctype html> <html ng-app="justChatting"> < ...

  4. C++ exercises Virtual functions - Calculate the area of the figure

    C++ exercises Virtual functions - Calculate the area of the figure Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 86 [cid=1143&pid=6 ...

  5. solve Windows Two tools for memory problems RamMap and VMMap( This one is better )

    source :http://www.cr173.com/html/13006_1.html .net Program memory monitor allocation tool (CLR Profiler for .NET Framework 4) Official installation version type : ...

  6. Jmeter_ Interface automation basic process overview

    Here is a basic operation , Otherwise, I always feel bottomless ! 1: Create a thread group   2: Add one cookie Manager   33333 3: Set up your header Manager :application/json;text/plain;ch ...

  7. mysql All kinds of operation commands

    1.mysql Command to log in form : mysql -u user name -p password mysql -uroot -proot 2.mysql Display database form : show databases; 3.mysql Enter a certain area ...

  8. jira7 Through the overall situation js Customize shortcut keys for edit area 【 primary 】

    jira7 Edit area custom shortcut key This paper mainly describes jira7 How to add global js quote , Customize the shortcut key for the text editing area to quickly fill in the template content . jira 3/4/5 Please refer to the official api https://developer ...

  9. git Local coverage with remote

    git  Local coverage with remote   git fetch --all git reset --hard origin/master

  10. Forms Authentication and Role based Authorization: A Quicker, Simpler, and Correct Approach

    https://www.codeproject.com/Articles/36836/Forms-Authentication-and-Role-based-Authorization Problem ...