In Android, Intent transmits the object [Serializable or Parcelable] between activities.

Source: Internet
Author: User

In Android, Intent transmits the object [Serializable or Parcelable] between activities.

Start activity with intent

/*** Serializeable Method for passing objects */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 object transfer method */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 );}

Receive

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);         }  }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.