Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 data. All my classes are Serializable and can saved and loaded between activities which seems to work thus far. But I'm predicting as more and more data is stored, the app will start running slow.

What I'm basically asking is there any way to retain this data throughout the entire application, so I don't have to load it every time a new screen pops up. I've already found the following information to help but it needs to be a little more clear for me to understand:

Another forum said you could stuff it in the Application object:

[Application]
public class MyApp : Android.App.Application {
    public MyApp(IntPtr handle)
        : base (handle)
    {
    }

    public FishingData Data {get; set;}
}

Then within your Activity:

((MyApp) this.ApplicationContext).Data = value;

So I've never really heard of doing this approach before and I'm not sure this will live through the entire application process (I feel like either way it's going to have to load the data via serialization. Here's what I want the app todo:

The first activity is the main menu and the following must be done when the screen loads:

  1. If a settings file is found, use serialization to load a previous FishingData object (I know how to do this)
  2. If not, then create a new clean FishingData object to save later (I know this as well)
  3. So now that we have a FishingData object, how do I ensure that I don't have to repeat steps 1-2 in every activity. How can I somehow pass the FishingData object to the next activity and ensure that it lives globaly while the app is still living. I only want to load it once (via serializing) (<--Don't know how to do this) and save it only when a user adds data to this object (which I know how to do).

Any help will be appreciated. This is bugging me I cant seem to figure this out. This seems like it would be a common thing to do but I haven't had any luck finding any detailed information.

like image 540
TeamChillshot Avatar asked Nov 29 '12 19:11

TeamChillshot


2 Answers

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

like image 87
Jameo Avatar answered Sep 30 '22 23:09

Jameo


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.

like image 45
Jong Avatar answered Sep 30 '22 23:09

Jong