Android intent transmits list or object

Source: Internet
Author: User
Method 1:
If you simply pass list <string> or list <integer>, you can directly use

Java code

Intent. putstringarraylistextra (name, value)

Intent. putintegerarraylistextra (name, value)


Method 2:
If list <Object> is passed, you can convert list to serializable type, and then use
Java code putextras (Key, (serializable) List)
Method passed, used for acceptance
Java code (list <yourobject>) getintent (). getserializable (key)
You can receive the list <yourobject> data.

But remember that your yourobject class must implement the serializable interface.

Method 3:
One is
Java code bundle. putserializable (Key, object );
The other is
Java code bundle. putparcelable (Key, object );
Of course, these objects have certain conditions. The former implements the serializable interface, while the latter implements the parcelable interface.


Method 4:
It is inconvenient to use intent to transmit data. We can write a global data in the application.

1. Create a subclass of your own Android. App. Application
2. Declare this class in manifest,
3. In this case, Android creates a globally available instance. You can use context. getapplicationcontext () to obtain the instance anywhere else, and then obtain the status (variable ).

Inherit Application
Java code

Class MyApp extends application {

Private string mystate;

Public String getstate (){

Return mystate;

}

Public void setstate (string s ){

Mystate = s;

}

}


For the configuration in androidmanifest. XML, you can directly add a name to the application, as shown below:
Java code <application Android: Name = ". MyApp" Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">

Use
Java code

Class blah extends activity {

@ Override

Public void oncreate (bundle B ){

...

MyApp appstate = (MyApp) getapplicationcontext ());

String state = appstate. getstate ();

...

}

}

 

* ** This is reproduced, but I feel more commonly used to open parameters here: http://hi.baidu.com/ihsauqaxblbdmwq/item/dfc9cf9c352b0bdf1a49dfd5

Related Article

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.