Efficiently Passing Custom Object Data between Android Activities [Mono Android]

前端 未结 2 1294
谎友^
谎友^ 2020-12-19 05:21

I\'ve been stumped with this for a while now. I\'m working on an android app that stores a person\'s fish catches, favorite fishing locations, tackle box inventory and other

相关标签:
2条回答
  • 2020-12-19 05:58

    You can use this approach, it will live as long as your Application object is alive (Which means it will live through your entire application and activities). You can read more about using global variables stored in the Application object here. I don't think mono would make a difference which will prevent you from using this approach.

    0 讨论(0)
  • 2020-12-19 06:02

    Here is how I would pass my data around the app via parcelable. Lets say you have a class named Fisherman (for a user basically)

    public class Fisherman implements Parcelable {
     private String name;
     private Tacklebox box;
     public int describeContents() {
         return 0;
     }
    
     public void writeToParcel(Parcel out, int flags) {
         out.writeString(name);
         out.writeParcelable(box, 0);
     }
    
     public static final Parcelable.Creator<Fisherman> CREATOR
             = new Parcelable.Creator<Fisherman>() {
         public Fisherman createFromParcel(Parcel in) {
             return new Fisherman(in);
         }
    
         public Fisherman[] newArray(int size) {
             return new Fisherman[size];
         }
     };
    
     private Fisherman(Parcel in) {
         name = in.readString();
         box = in.readParcelable(com.fisher.Tacklebox);
     }
    }
    

    In this example, you define parcelable for each data model you have. So say you have a fisherman object, that contains another object called tacklebox. You will also define this for tacklebox, and so on if you continue to nest models. This way, all you need to do to pass data between activities is

    Intent intent = new Intent(this, Activity.class);
    intent.putParcelableExtra("com.fisher.Fisherman", fisherman);
    

    and read

    Bundle b = getIntent().getExtras();
    Fisherman fisher = b.getParcelable("com.fisher.Fisherman");
    

    This unfortunetly answers only step 3 of your problem, but I suggest breaking each one of your 3 steps into its own question because what your trying to do is slightly more lengthy than one question

    0 讨论(0)
提交回复
热议问题