Effectively transfer user object data between Android functions [Mono Android]

I have been at a standstill with this for a while. I am working on an android application that stores peopleโ€™s tricks, favorite fishing spots, inventory and other data. All my classes are Serializable and can be saved and loaded between actions that seem to work so far. But I predict that more and more data is saved, the application will start to work slowly.

Basically I ask if there is a way to save this data throughout the application, so I donโ€™t need to download it every time a new screen appears. I already found the following information to help, but it should be clearer to me:

Another forum said that you can write it in the Application object:

[Application] public class MyApp : Android.App.Application { public MyApp(IntPtr handle) : base (handle) { } public FishingData Data {get; set;} } 

Then, as part of your work:

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

Therefore, I have never heard of this before, and I'm not sure that this will go through the whole process of the application (I feel that somehow I will have to load data through serialization. Here is what I want todo application:

The first action is the main menu, and when loading the screen, you must do the following:

  • If the settings file is found, use serialization to load the previous FishingData object (I know how to do this)
  • If not, create a new clean FishingData object to save later (I know that too)
  • So, now that we have the FishingData object, how can I guarantee that I do not need to repeat steps 1-2 in each action. How can I somehow pass the FishingData object for the next action and make sure that it lives globally while the application is still living. I want to download it only once (via serialization) (<- I donโ€™t know how to do this) and save it only when the user adds data to this object (which I know how to do).

Any help would be appreciated. It pushes me, I can not understand it. This seemed to be commonplace, but I could not find detailed information.

+7
source share
2 answers

You can use this approach, it will live as long as your Application object is alive (this means that it will live through all your application and actions). Read more about using global variables stored in the Application object here . I do not think that mono will have a value that will not allow you to use this approach.

+1
source

Here is how I can transfer my data in the application whenever possible. Let's say you have a class called Fisherman (for the 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 the capability for each data model that you have. So say that you have a fisherman object that contains another object called tacklebox. You will also define this for tacklebox etc. if you continue to build models. Thus, all you have to do to transfer data between actions 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, unfortunately, answers only step 3 of your problem, but I suggest hacking each of the three steps into your own question, because what you are trying to do is somewhat longer than one question

+3
source

All Articles