So basically, I can't shake the fact that this seems like a bad idea, but I can't really determine why.
I have Activity A
and Activity B
in Package 1
and Activity A
makes a Volley call and creates a list of models Model X
that contains some simple data (Strings and primitives). For a very simple example, let Model X
represents a news article (so the list of models is a list of news articles) which contains a news article's title and body. Activity A
contains a list that displays the title of the articles. When you click on an item in the list, Activity B
is called and the title of the article is shown at the top and the body of the article is shown below. Activity B
can't be called from anywhere but Activity A
and only after A has retrieved Model X
Now let's say we have a large number of these models that all contain a large number of simple types and you aren't exactly sure what data from the model Activity B
needs, you just know that it needs Model X
that was retrieved in Activity A
.
The "correct" step, it seems, would be to have the models implement Parcelable and pass the model in a bundle to the activity. The thing about that is that I have a lot of models with a lot of data and if I can save time it would be great. I was trying to implement some sort of generic parceler and that was getting to be a mess. So here is what I thought:
Could
Activity A
not save the model selected in onItemClick() as a protected static variable thatActivity B
can use without having to pass it in a parceled bundle since, once the model is made, it will be the only instance of the model and the model will not be altered in any way so the state of the model can't be disrupted by the static call?
Something like this
Activity A
public class A extends Activity implements OnItemClickListener{
private List<X> mModels;
protected static X mModel;
private SimpleListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// code
if(mModels == null)
requestModels();
else
buildInitialList();
}
@Override
public void onItemClick(AdapterView<?> parent, View child, int position, long id){
mModel = mAdapter.getItem(position); // <-- returns Model X in list of models
startActivity(new Intent(this, B.class));
}
/*
Some irrelevant code, including requestModels() network call (I have a
NetworkManager class that does that, but again it isn't important how it
does that, just important how I get my data) as well as building my list.
*/
// Listener attached to Volley call for response
private Listener<ArrayList<Model>> getVolleyListener(){
return new Listener<ArrayList<Model>>(){
@Override
public void onResponse(ArrayList<Model> models) {
if(models != null){
mModels = models;
buildInitialList();
}
else
closeOnEmptyList();
}
};
}
}
Activity B
public class B extends Activity{
private X mModel;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//code
this.mModel = A.mModel;
doSomethingWithModel();
}
private void doSomethingWithModel(){
String title = mModel.title;
String body = mModel.body;
// do something
}
}
The Model class X will never be used outside of Activity A and Activity B, and every Activity that needs to use a Model of the same type is placed in the same Package
. I have a number of classes that do similar things (news articles, popular questions and answers, law details, just other generic String data for the most part). I can parcel everything that isn't an issue, I just can't seem to put my finger on what is wrong with this even though it seems flawed. None of the data needs any security and none of the models are large enough to cause an issue with static heap memory.
EDIT
My case isn't exactly unique, but is somewhat uncommon. I am ok with my data having to be repulled at the restart of the application. This means (to the best of my knowledge) that if I store the Model statically of the protected type in Activity A, there is no way that the Model could be null in Activity B
as either the static variable is still persistent in the application from the still paused Activity A
or the application has restarted and the user will have to go through Activity A
rebuilding our volley Model
so that Activity B
can use it as well. (Possible error in logic check needed here)
I do realize that volley has a cache, but I am fairly certain it requires the correct cache headers (that give a response code of something like 304) which I am not getting. Any input on this is also appreciated. And again, I realize that using statics isn't exactly ideal, but I am really trying to figure out why they shouldn't be used in this case, or if this is actually one of the uses for them.