当前位置: 代码迷 >> Android >> Android中Intent在Activity其间传递对象[Serializable或Parcelable]
  详细解决方案

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

热度:49   发布时间:2016-04-24 11:39:44.0
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);         }  }  
  相关解决方案