Sounds like you need a data model to hold the data between activities.
One solution would be to create a static class or use the singleton design pattern. This could take the result from the service. You would initialise this in Activity A and start the service before firing the intent for Activity B
Then in activity B you can send a call a method to register it's call back function. If the data is there come back straight away to the call back function. If not do so when your new class is called back from the function.
Your data is then shared and only fetched/refreshed as and when you need it to be.
// ----------- Class to hold your data Item -----------
public class DataItem {
private Integer id = -1;
// My Data stuff
public Integer getId() {
return this.id;
}
}
// ----------- Interface to join it all together -----------
import java.util.ArrayList;
public interface NotifiyDataInterface {
public void completedDataLoad( ArrayList data, String status);
}
// ----------- Singleton Class for the datafetching -----------
public class DataFetcher {
static DataFetcher _instance = null;
ArrayList<NotifiyDataInterface> _callBackList = null;
ArrayList<DataItem> _cachedData = null;
String _dataStatus = "";
public DataFetcher() {
_callBackList = new ArrayList<NotifiyDataInterface>();
}
public static DataFetcher getInstance() {
if (DataFetcher._instance == null) {
DataFetcher._instance = new DataFetcher();
}
return _instance;
}
public void fetchData(NotifiyDataInterface _callBack) {
if (_cachedData != null) {
_callBack.completedDataLoad(this._cachedData, this._dataStatus);
return;
}
// Add to the call back list
this._callBackList.add(_callBack);
// Code to call your service to get data
//TODO: Add code to call your service
}
public void dataLoadComplete(ArrayList<DataItem> _newItems, String _fetchStatus) {
// Called from the service on a completed data load
this._cachedData = _newItems;
this._dataStatus = _fetchStatus;
NotifiyDataInterface _item = null;
for (int i = 0; i < _callBackList.size(); i++) {
_item = _callBackList.get(i);
_item.completedDataLoad(this._cachedData, this._dataStatus);
}
// Clear out the call back list
_callBackList.clear();
}
}
// ----------- Class for activity B -----------
public class ActivityB extends ActionBarActivity implements NotifiyDataInterface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
// Get the single instance from the singleton pattern
DataFetcher dataFetcher = DataFetcher.getInstance();
dataFetcher.fetchData(this);
}
// Here is where you call back is fired
public void completedDataLoad( ArrayList data, String status) {
//TODO: Your Code to call on data load here
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.layout.activity_b, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}