Android passes an object from one activity to another activity

Source: Internet
Author: User

In Android, it is often necessary to pass a value or object from one activtity to another activity.

First of all, there is a class, this class must go to implement Serializable; The code is as follows

public class Student implements serializable{String name;}

In the first activity, the object is passed as a value in intent, and the code is as follows

Intent Intent = new Intent (mainactivity.this,secondactivity.class); Student s = new Student (); s.name = "Wahaha"; Intent.putextra ("Stu", s); startactivity (intent);

To receive this object in the second activity.

TextView = (TextView) Findviewbyid (R.id.textview); Intent Intent = Getintent (); Serializable extra = Intent.getserializableextra ("Stu"); Student s = new Student (); String SName = null;//If the downward transition to Student class if (extra instanceof Student) {//Forced transformation SName = ((Student) extra). Name;} Textview.settext (SName);

So the object is passed over.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android passes an object from one activity to another activity

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.