Friday, May 25, 2012

Pass Custom Object Using Serializable : Android


Serialization is a process of reading or writing an object. It is a process of saving an object’s state to a sequence of bytes, as well as a process of rebuilding those bytes back into a live object at some future time. An object is marked serializable by implementing the java.io.Serializable interface, which is only a marker interface -- it simply allows the serialization mechanism to verify that the class can be persisted, typically to a file.
Transient variables cannot be serialized. The fields marked transient in a serializable object will not be transmitted in the byte stream. An example would be a file handle, a database connection, a system thread etc. Such objects are only meaningful locally. So they should be marked as transient in a serializable class.
Serialization can adversely affect performance since it:
  • Depends on reflection.
  • Has an incredibly verbose data format.
  • Is very easy to send surplus data..

Example Pass Custom Object by implementing Serializable interface:
   Create Person.java an Serializable class: :
package com.imrankhanandroid.Passdserialzedata;

import java.io.Serializable;

//Person Serializable Class
public class Person implements Serializable {
 //serialVersionUID generated by serialver tool.
 private static final long serialVersionUID = 10275439472837494L; 
    private String name;  
    private int 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;  
    }  
}

   Create ObejctSenderActivity Activity: :
package com.imrankhanandroid.Passdserialzedata;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ObejctSenderActivity extends Activity {
 private Button sButton;  
    public  final static String SER_KEY = 
            "com.imrankhanandroid.Passdserialzedata.ser";  

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sButton = (Button)findViewById(R.id.btnsend); 
        sButton.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    SerializeMethod();
   }
  });
    }
  //Serializeable Passing objects  
    public void SerializeMethod(){ 
     //Create an mPerson Object of Person Class
        Person mPerson = new Person();  
        mPerson.setName("Imran Khan");  
        mPerson.setAge(25);  
        //Create an Intent for Passing to startActivity
        Intent mIntent = new Intent(this,ObjectReceiverActivity.class);  
        Bundle mBundle = new Bundle();  
        //put mPerson in bundle using putSerializable
        mBundle.putSerializable(SER_KEY,mPerson);  
        mIntent.putExtras(mBundle);  
          
        startActivity(mIntent);  
    }  
}

   Create ObjectReceiverActivity Activity: :
package com.imrankhanandroid.Passdserialzedata;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;


public class ObjectReceiverActivity  extends Activity{
 TextView txtview;
  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maintwo);
        //Get person object from Intent deliver to ObjectReceiverActivity
        Person mPerson = (Person)getIntent()
        .getSerializableExtra(ObejctSenderActivity.SER_KEY);
        txtview=(TextView)findViewById(R.id.txtrev);
        txtview.append(Html.fromHtml("
Person Name :"+mPerson.getName()
          +"
Person Age :"+mPerson.getAge()));
    }
}

   res/layout/main.xml: :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView    
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:text="Welcome to Imran Khan's Blog."
    android:gravity="center"  
    android:layout_marginTop="80dp"
    />  
    <TextView    
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:text="Object Sender Activity"
    android:gravity="center"  
    android:layout_marginTop="80dp"
    /> 
<Button  
    android:id="@+id/btnsend"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="Send Serializable Object" 
    android:textSize="25px" 
    android:layout_marginTop="15dp"
/>  

</LinearLayout>


   res/layout/maintwo.xml: :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView    
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:text="Obecjt Reciver Activity"
    android:gravity="center"  
    android:layout_marginTop="80dp"
    android:textSize="25px"
    /> 
    <TextView    
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:text="Data Recived from Sender Activity:-"
    android:layout_marginTop="80dp"
    android:id="@+id/txtrev"
    android:textSize="25px"
    /> 
</LinearLayout>


   res/layout/maintwo.xml: :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.imrankhanandroid.Passdserialzedata"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="Welcome to Imran Khan's Blog."
            android:name=".ObejctSenderActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ObjectReceiverActivity"></activity>
    </application>

</manifest>

Download Source Code: PassdatausingSerializable









0 comments:

Post a Comment

 
Powered by Blogger