Say I have an reference to an object, how should I go about passing this from one Activity to another?
I don't want to have to query the Application Object / singletons / static variables.
Is this still possible?
You can declare a static variable in another activity, or some Global Variable in Application class, then access it to any activity, like you want to parse some Object of Type NewType, to Class NewActivity, from OldActivity. Do as Following:
Declare an Object of Static NewType in NewActivity.java.
public static NewObject newObject=null;
do Following, when you invoke NewActivity.
NewActivity.newObject=item;
Intent intent=new Intent(OldActivity.this, NewActivity.class);
startActivity(intent);
You can do it in one of the following ways :
Here's an eg from the docs : you can wrap your object with a parcelable
, attach it to an intent, and 'un-wrap' it in the recieving activity.
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
One of the Solution:
You can make a singleton class to carry the information you want
for example:
StorageManager.getInstance().saveSomething(Object obj);
then retrieve back with respective getter method
Make sure you take care of the synchronization issues;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.