How To Pass Object To A New Activity? (how To Inject)
Solution 1:
MyParcelable.java
publicclassMyParcelableimplementsParcelable {
privateString mVal;
publicMyParcelable(String val) {
mVal = val;
}
publicstatic final Parcelable.Creator<MyParcelable> CREATOR =
newParcelable.Creator<MyParcelable>() {
publicMyParcelablecreateFromParcel(Parcel in) {
String s = in.readString();
returnnewMyParcelable(s);
}
publicMyParcelable[] newArray(int size) {
returnnewMyParcelable[size];
}
};
publicStringgetValue() {
return mVal;
}
publicvoidwriteToParcel(Parcel p) {
p.writeString(mVal);
}
publicStringtoString() {
return"MyParcelable[val=" + mVal + "]";
}
}
SendParcelable.java
publicclassSendParcelableextendsActivity {
@OverridepublicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
Intentintent=newIntent(this, ReceiveParcelable.class);
Parcelablep=newMyParcelable("test");
intent.putExtra("myparcelable", p);
Log.i("ParcelDemo", "Sending Parcelable");
startActivity(intent);
}
}
ReceiveParcelable.java
publicclassReceiveParcelableextendsActivity {
@OverridepublicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
Intentintent= getIntent();
Parcelablep= intent.getParcelableExtra("myparcelable");
Log.i("ParcelDemo", "Got Parcelable " + p);
if (p != null && p instanceof MyParcelable &&
((MyParcelable) p).getValue().equals("test")) {
Log.i("ParcelDemo", "Success!");
} else {
Log.i("ParcelDemo", "Failure!");
}
}
}
Hope this helps!
Solution 2:
There is an entry in Android documentation - How do I pass data between Activities/Services within a single application? If you're talking about non-persistent objects, your options will be:
- Singleton class
- A public static field/method
- A HashMap of WeakReferences to Objects
By the way, I don't really understand the distinction you make between objects and data. An object contains fields and a set of methods to work with them. However, it doesn't matter if this object has some methods or not, they do not make a part of its state. If you pass an object, you only need to pass its state, so you can save the state using serialization (through Serializable or Parcelable interface) and then restore it.
Solution 3:
If you are just trying to find a way to pass objects between activities you can override the Application class.
Way to use application class.
Extend the application class and add your object as its attribute. In any activity, if you call the below code, it will return a singleton.
MyApplicationappContext= (MyApplication) getApplication();
To make this work you need to add this to the application tag of the manifest file
android:name=".MyApplication"
This method is used to pass values and objects around the app.
Solution 4:
Parcelable is an option. It will write the data from your object and create a new object from the data. Its like saving and loading data.
Take a look at http://developer.android.com/reference/android/os/Parcelable.html to see how you should implement it.
Solution 5:
For now, i did for destination Activity:
StringclassName= getIntent().getStringExtra("com.myproject.ModelClass");
IModelmodel= (IModel)Class.forName(className).newInstance();
And i have a specific implementaction in the Activity.
Post a Comment for "How To Pass Object To A New Activity? (how To Inject)"