7

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.

patridge
  • 26,385
  • 18
  • 89
  • 135
TeamChillshot
  • 157
  • 3
  • 12
  • I have no idea, but +1 for an extremely well formatted first question. Welcome to StackOverflow. – Phillip Schmidt Nov 29 '12 at 19:22
  • To pass information between activities, you want to use Parcelable http://developer.android.com/reference/android/os/Parcelable.html However, to save all this data, optimally you would at least create an sqllite database, or more ideally use your own database – Jameo Nov 29 '12 at 19:38
  • Check out http://forums.xamarin.com/discussion/451/communicate-with-iserializable – Cheesebaron Nov 29 '12 at 23:45

2 Answers2

3

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

patridge
  • 26,385
  • 18
  • 89
  • 135
Jameo
  • 4,507
  • 8
  • 40
  • 66
1

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.

Community
  • 1
  • 1
Jong
  • 9,045
  • 3
  • 34
  • 66
  • Thanks, I tested it out using steps from the above link and it is now successfully keeping a global variable stored throughout activities. Thanks for your help, this is a huge weight off my shoulders. – TeamChillshot Dec 01 '12 at 15:55
  • Glad it helped :) Please mark the question as solved: http://stackoverflow.com/faq#howtoask – Jong Dec 01 '12 at 19:46
  • 1
    The application object is guaranteed to be created before all your activities and kept until all your activities are destroyed. You can't use it for global state though because Android can destroy all your activities AND the application object when your application is in the background. – Jared Kells Apr 19 '13 at 03:35
  • 1
    If you receive a phone call OR switch applications which could just mean launching a browser intent the OS can optionally destroy all your activities, destroy your application object and kill your hosting process. When you end the phone call a new process will start, a new application object will be created and a new activity will be created. The only state you will get back will be in the bundle passed into onCreate. All your static variables will be null and your Application object will be a brand new instance. – Jared Kells Apr 19 '13 at 03:40