Android Intent passing list or object

Source: Internet
Author: User

Method One:
If you simply pass list<string> or list<integer>, you can use it directly.

Java code

Intent.putstringarraylistextra (name, value)

Intent.putintegerarraylistextra (name, value)


Method Two:  
If you are passing list<object>, you can convert the list to serializable type and then through  
Java code    Putextras (key,  (Serializable) list)   
method passed past, accepted with  
Java code    (list< yourobject>)  getintent (). getserializable (key)   
to receive list<yourobject> data  

But remember that your yourobject class must implement Serializable interface  

Method Three:  
one is  
Java code    Bundle.putserializable (key,object);   
another is  
Java code   bundle.putparcelable (Key,  object);   
Of course, these objects have certain conditions, the former is the implementation of the serializable interface, and the latter is the implementation of the Parcelable interface  


Method four:  
used intent to feel inconvenient we can write a global data   in application;

1. Create a subclass of your own android.app.Application  
2, declare this class in manifest,  
3. This is where Android builds a globally available instance that you can use Context.getapplicationcontext anywhere else () method to obtain the state (variable) of the instance.  

Inherits application 
Java code   

Class MyApp extends Application {

Private String MyState;

Public String getState () {

return mystate;

}

public void SetState (String s) {

MyState = s;

}

}


Regarding the configuration in the Androidmanifest.xml, it is possible to add a name directly to 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 feel that they are more commonly used to facilitate participation in the open here: http://hi.baidu.com/ihsauqaxblbdmwq/item/dfc9cf9c352b0bdf1a49dfd5

Android Intent passing list or object

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.