码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-03-02 11:16:14      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

使用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]

标签:

原文地址:http://blog.csdn.net/taoolee/article/details/50777864

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!