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.