Intent the way to pass list or object in Android _android

Source: Internet
Author: User

The example in this article describes the way the intent passes list or object in Android. Share to everyone for your reference. The implementation methods are as follows:

Method One:

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

The code is as follows:

Copy Code code as follows:
Intent.putstringarraylistextra (name, value)
Intent.putintegerarraylistextra (name, value)

Method Two:

If the pass is LIST<OBJECT>, you can turn the list strong to serializable type and then pass

Copy Code code as follows:
Putextras (Key, (Serializable) list)

Methods pass past, accept the time with
Copy Code code as follows:
(list<yourobject>) Getintent (). getserializable (Key)

You'll be able to accept the list<yourobject> data.

But remember that your yourobject class has to implement the Serializable interface

Method Three:

One is

Copy Code code as follows:
Bundle.putserializable (Key,object);

Another kind is
Copy Code code as follows:
Bundle.putparcelable (Key, Object);

Of course, these object is a certain condition, the former is to implement the serializable interface, and the latter is the implementation of the Parcelable interface

Method Four:

It's not convenient to pass the intent. We can write a global data in application.

1. Create a subclass of your own android.app.Application
2, in the manifest to affirm this class,
3, then Android to create a global available instance, you can use the Context.getapplicationcontext () method anywhere else to get this instance, and then get the state (variable).

Inherit application

The code is as follows:

Copy Code code as follows:
Class MyApp extends Application {
Private String MyState;
Public String getState () {
return mystate;
}
public void SetState (String s) {
MyState = s;
}
}

About the configuration in Androidmanifest.xml, the original directly to application add a name on it, as shown in the following:

Copy Code code as follows:
<application android:name= ". MyApp "android:icon=" @drawable/icon "android:label=" @string/app_name ">

Use

Copy Code code as follows:
Class Blah extends Activity {
@Override
public void OnCreate (Bundle b) {
...
MYAPP appstate = ((MyApp) Getapplicationcontext ());
String state = Appstate.getstate ();
...
}
}

I hope this article will help you with your Android program.

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.