How to Use bundle to transmit data?

Source: Internet
Author: User
How to Use bundle to transmit data? Recently, when I was learning Android, I don't know how to use bunndle to transfer objects? We can implement this through serizlizable. The essence is to convert a custom object into a serizliable object. And then obtain it when it is used. Stored data void putserializable (string key, serializble value); Data: serializanle getserizlizble (string key );

Code: mianactivity. Java
Package COM. example. serilizaldemo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; public class mainactivity extends activity {private textview title; private textview URL; private button; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); Title = (textview) This. findviewbyid (R. id. title); url = (textview) This. findviewbyid (R. id. URL); button = (button) This. findviewbyid (R. id. button1); Final game = new game (); game. settitle (title. gettext () + ""); game. seturl (URL. gettext () + ""); button. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubintent intent = new intent (mainactivity. this, secondactiviity. class); // 1 encapsulate serialized bundle = new bundle (); bundle. putserializable ("love", game); intent. putextra ("game", bundle); startactivity (intent) ;}}) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true ;}}

Secondactivity
Package COM. example. serilizaldemo; import Java. io. serializable; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. util. log; public class secondactiviity extends activity {Private Static final string tag = "secondactiviity"; Public secondactiviity () {// todo auto-generated constructor stub} @ overrideprotected void oncreate (bundle savedinstancestate) {// todo Auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. second); intent = getintent (); bundle = intent. getbundleextra ("game"); // 2 deserialization! Retrieve data. Serializable OBJ = bundle. getserializable ("love"); If (OBJ! = NULL & OBJ instanceof game) {game G = (game) OBJ; log. I (TAG, "-- url =" + G. geturl (); log. I (TAG, "-- Title =" + G. gettitle ();} log. I (TAG, "-- Title = ");}}

3 game serialization
Package COM. example. serilizaldemo; import Java. io. serializable; import Java. util. list; // implement serializable implementation interface public class game implements serializable {// define your own serial number private final static long serialversionuid = 1234567890l; private String title; private string URL; private long filesize; // Private list <go> list; Public game () {// todo auto-generated constructor stub} Public String gettitle () {return title ;} public void settitle (String title) {This. title = title;} Public String geturl () {return URL;} public void seturl (string URL) {This. url = URL;} public long getfilesize () {return filesize;} public void setfilesize (long filesize) {This. filesize = filesize ;}}


Summary: How does bundle pass objects?
Serialization and deserialization
Encapsulate objects through serialization.
Parses objects through deserialization.
How can custom classes be serialized?
Is to implement serializable
Private Final Static long serialversionuid = 1234567890l;


Complimentary:
Basic Data and sets are serializable,
Custom classes can be serialized only when serializable is implemented!



How to Use bundle to transmit data?

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.