I am currently working on an Android app (I am a beginner in developing Android apps) and I am implementing a very simple class which is able to handle a location (latitude, longitude) and the corresponding address (by the use of the Geocoder API). I want my app to be able to transfer one instance of my class between two activities and also to get the result back, such result being also an instance of that class.
I have written the declaration of my class to implement the Parcelable class, like this (my class name is OnePlace):
public class OnePlace implements Parcelable {
private LatLng position;
private String address;
…
}
I have implemented the "OnePlace" class with the methods to be able to implement a Parcelable as it is recommended on the Android developper online documentation: https://developer.android.com/reference/android/os/Parcelable
Everything works fine and I am able to transfer one instance of my class from one activity to another and to get another instance back as a result.
Here is the snippet code in which I add the instance of my class as a data sent along with the intent:
OnePlace item = new OnePlace(); // create an instance of the class with default values
Intent mapIntent = new Intent(adapterView.getContext(), MapsActivity.class);
mapIntent.putExtra("place", item); // add the item as extra data to the intent
startActivityForResult(mapIntent, 35);
In the called activity (here MapsActivity), I am able to rebuild an exact copy of the class instance.
Anyway, this works and I am happy with that.
Now, I would like to store the instance of my class in a file that will be read when I start my app the next time (a way to have permanent storage).
For that, I use the following code snippet inside the finish() method of my activity (just before leaving my app):
FileOutputStream outStream = openFileOutput("myFile.tmp", Context.MODE_PRIVATE);
ObjectOutputStream objectStream = new ObjectOutputStream(outStream);
objectStream.writeObject(item);
objectStream.close();
outStream.close();
As you can see, I use an ObjectOutputStream class with a FileOutputStream class to be able to write into the file. The item object is one instance of my OnePlace class. It is that instance that I want to permanently store into a file to be able to get it back next time.
When I call the writeObject(…) method, the call ends with an exception "Object not serializable". After reading some online documentation, it is clearly stated that the class I want to "serialize" shall implement the Serializable interface (this is mandatory!).
Consequently, I updated the definition of my class like this:
public class OnePlace implements Parcelable, Serializable {
private LatLng position;
private String address;
…
I slightly modify the definition of my class to implement both Parcelable and Serializable interfaces. I do not know if it the best way to achieve my needs…
When I want to call the intent.putExtra("key", item);, I get an error. In fact, the Java compiler complains about the fact that it does not know which putExtra interface it shall implement: the one with the Parcelable or the one with the Serializable interface ? Its reaction seems to be logical and I think that if I have been a human compiler, I probably would have asked the same question.
So, now, how can I do to solve that issue ? Anybody has an idea ? Maybe, the way I want to achieve my two goals (transfer class instance to another activity and permanently store an instance) is not the best way ? Do you have some suggestions ?
I thank you a lot in advance for your answers.
Charles