问题
Here is my situation. I have written a Playlist class that stores a context. Playlist has 9 child classes. Unfortunately, to pass Playlist between intents, it must implement Serializable. This is a problem because Playlist stores a context, so that the Iterator can work, as the Iterator method that must be Overridden from Iterator can not accept any parameters. Therefore, I must store Context somewhere as it is needed to determine the size of the Playlist. Here is the (simplified) code.
public abstract class Playlist implements Serializable, Iterable<Song>
{
private static final long serialVersionUID = 0L;
private Context context;
public Context getContext() { return context; }
public Playlist(Context context)
{
this.context = context;
}
public abstract int size(); //getContext() referenced in all currently written children
public abstract Song getSong(int index); //getContext() referenced in all currently written children
@Override
public PlaylistIterator iterator()
{
return new PlaylistIterator();
}
public class PlaylistIterator implements Iterator<Song>
{
private int current;
PlaylistIterator()
{
current = 0;
}
@Override
public boolean hasNext()
{
return current < size(); //SIZE HERE needs access to a context, but this method certainly can not take one, and neither can the constructor above.**
}
@Override
public Song next()
{
if (!hasNext())
throw new NoSuchElementException();
return getSong(current++);
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
}
}
I have read that you can store a static context, but that this is poor design. I can not seem to find a way around this.
I have considered adding a static context reference that is assigned in writeObject, and then accessed in readObject, as the transition should be nearly instant because the Serialization implementation is only so that Playlist can be passed in an intent. But even this feels hacky.
Is there a common work around to dealing with the fact that we cannot serialize context? Is my solution acceptable in terms of stability? It might be against the rules, but what is your recommendation in this situation?
回答1:
I have written a Playlist class that stores a context
That's probably not a good idea.
Unfortunately, to pass Playlist between intents, it must implement Serializable
It could be Parcelable
, but that does not solve your problem. A Context
cannot go into either a Serializable
or a Parcelable
.
because Playlist stores a context, so that the Iterator can work,
That's probably not a good idea.
Therefore, I must store Context somewhere as it is needed to determine the size of the Playlist.
Or, the Playlist
could hold the size of the playlist. An int
is readily able to be used with Serializable
or Parcelable
.
Or, get rid of the Iterator
, as that is not going to work well with Serializable
or Parcelable
either.
I can not seem to find a way around this.
Have Playlist
be a pure model object, with no Context
.
Or, depending on the use case, have Playlist
be a singleton, using the Application
as the Context
. It is unclear whether there is only one Playlist
or several.
but what is your recommendation in this situation?
Playlist
should not hold a Context
and should not have an Iterator
.
来源:https://stackoverflow.com/questions/37231573/android-context-not-serializable-dilemma-for-class-that-implements-iteratable