0

I am using an itemizedOverlay within my MapView that is generating waypoints. I have the ability to modify, add, delete these waypoints as desired. This MapView shares the screen as a fragment with a listView fragment. Currently the purpose of the fragment is to display the waypoints from the map and allow them to be deleted, moved, or whatever using the listFragment. All of this works as of right now.

The issue I have is that both fragments hold their own copy of the waypoint list. I.E. if I delete a waypoint using the mapview, the itemizedOverlay class must call the ListFragment function to remove a waypoint from its own list. This gets ugly and will have its own problems soon. It would be better to have a single list either in the listView, itemizedOverlay, or a separate class file.

In other applications (C++ or Java based), I have typically created another class as a singleton and it holds the information that would be shared between the map and listview. Is this still the best way to handle the issue when working with Android, or is there another better, built-in option to use?

orion
  • 89
  • 2
  • 7

1 Answers1

0

You can use a single instance of your list if you hold a reference to a class that extends the Application class of your project. See here for more information about this approach.

Community
  • 1
  • 1
AggelosK
  • 4,313
  • 2
  • 32
  • 37
  • You answer works, however, I have been suggested that it is best to make the object Parcelable. This is quite simple, and in the end requires me to pass the object to the fragment using bundles. I know both methods are easy, except the Parcelable method has its own nuance of requiring a coder to order the items being placed in the parcel in a specific order. Is there a better option? It seems android prefers Parcelable. – orion Sep 17 '12 at 19:29
  • Another way i think it will work is to hold your waypoint list reference in the `Activty` that holds your two `Fragments` and access your waypoint list with get and set methods. You can get the associated `Activity` of a `Fragment` using the [getActivity](http://developer.android.com/reference/android/app/Fragment.html#getActivity%28%29) method. – AggelosK Sep 18 '12 at 06:48
  • I tried that as well as working with the application method. I am actually able to do all three methods, however, I am just unsure of which method is the best/cleanest. – orion Sep 18 '12 at 16:36
  • I have not tried the `Parceable` method but i have tried the other two proposed so regarding them i would propose to use the method in which your data is being held in your `Activity` since this way the lifecycle of your data will last at maximum as your `Activity` lifespan, and that can be more memmory efficient than the other method. The advantage of holding your data in the `Application` context is that the can be accesed from anywhere in your app as long as you have a `Context` reference, also their lifespan match your app lifespan. I dont think there is a definite answer in your question. – AggelosK Sep 18 '12 at 18:28
  • It does seem that way. I guess I will just keep my options open in case I need to switch from one or the other. Thanks. – orion Sep 18 '12 at 20:30