Android中Intent在Activity之间传递对象[Serializable或Parcelable]

使用intent启动activity

  /** * Serializeable传递对象的方法 */  
    private void SerializeMethod(){  
        Person mPerson = new Person();     
        mPerson.setName("andy");     
        mPerson.setAge(26);     
        Intent mIntent = new Intent(this,SerializableDemo.class);     
        Bundle mBundle = new Bundle();     
        mBundle.putSerializable(SER_KEY,mPerson);     
        mIntent.putExtras(mBundle);     

        startActivity(mIntent);     
    }  

    /** * Pacelable传递对象方法 */  
    private void PacelableMethod(){  
        Police mPolice = new Police();     
        mPolice.setName("I am Police");        
        mPolice.setWorkTime(2008);     
        Intent mIntent = new Intent(this,ParcelableDemo.class);     
        Bundle mBundle = new Bundle();     
        mBundle.putParcelable(PAR_KEY, mPolice);     
        mIntent.putExtras(mBundle);     

        startActivity(mIntent);     
    }  

接收

public class SerializableDemo extends Activity {  

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  

        TextView mTextView = new TextView(this);     
        Person mPerson = (Person)getIntent().getSerializableExtra(TestActivity.SER_KEY);     
        mTextView.setText("You name is: " + mPerson.getName() + "/n"+     
                          "You age is: " + mPerson.getAge());     

        setContentView(mTextView);     
    }  

}  
public class ParcelableDemo extends Activity {  

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  

        TextView mTextView = new TextView(this);     
        Police mPolice = (Police)getIntent().getParcelableExtra(TestActivity.PAR_KEY);     
        mTextView.setText("Police name is: " + mPolice.getName()+"/n"+     
                          "WorkTime is: " + mPolice.getWorkTime() + "/n");     
        setContentView(mTextView);     
    }  

}  

你可能感兴趣的:(Android中Intent在Activity之间传递对象[Serializable或Parcelable])