I have an Activity that extends FragmentActivity
In first Fragment (HomePage that extends Fragment) I need to load data from web, so I use ASyncTask.
Now the problem: I don't need to check and load data everytime Fragment is created/attach
but just in first instance, then I want to save state and load again data only on next onCreate Activity (That I hope it means next open of app, but we know that it doesn't)
So.. Where Should I put my ASyntTask.execute()? OnCreate inside Fragment? OnCreateView inside Fragment? On Attach inside Fragment? or OnCreate of Activity, store data in Bundle and then pass them to my Fragment?
Or, maybe other solutions to this?
EDIT
Resolved!
So on my OnCreate [Fragment] I added a
if(savedInstanceState != null) {
data = (data[]) savedInstanceState.getParcelableArray("key");
}
else
new LoadMostBooks().execute();
But I didn't setInstanceRetain(True) cause I need to pass always through onCreate();
Then on onCreateView() Method I added also a check
if(savedInstanceState != null) {
myCustomArrayAdapter = new CustomArrayArrayAdapter(mContext,data);
myListView.setAdapter(myCustomArrayAdapter);
}
I also needed to make sure about save instance with a
@Override
public void onSaveInstanceState( Bundle outState ) {
super.onSaveInstanceState( outState );
outState.putParcelableArray("key",data);
}
At the end I had to make data (Custom Object) implemented Parcelable to make sure everythign works!