Title: Activity that can access thread on a Service
Tags: android;android-activity;service
Question: I originally had an Activity that creates a thread which connects to a server, updates itself send the update to the server which then forwards the update to another device that is also running the app. I have run into the issue where the screen rotation creates another network thread. Note: that I cannot just lock the screen to prevent it from rotating.
I was directed to create a background service(as an alternative but not imperative if it can function another way) which runs the network thread. Then get the activity once its reconstructed to rebind to the service and continue to function as normally.
My question is how do I pass the context of my activity to the thread, and also access a variable inside that thread from my activity? I assume that someone will say create a class inside the service that runs the thread, and access the the field that is inside that class via a getter. I am completely new to services, however I am able to start it and run the thread. I am just confused on how to callback and forth between the activity and the thread.
EDIT I am getting and error its saying that I cannot cast Fragment in to my class Fragment
@Override
protected void onResume(){
FragmentManager manager =  getFragmentManager();
myFrag = (NetworkFragment) manager.findFragmentByTag(TAG_NETWORK_FRAGMENT); // here
if (myFrag == null) {
myFrag = new NetworkFragment();
manager.beginTransaction().add(myFrag,  TAG_NETWORK_FRAGMENT).commit(); // and it can't resolve this...
}
Fragment
import android.support.v4.app.Fragment;
public class NetworkFragment extends Fragment implements Runnable {
//blahbalh
Comment: Don't pass an activity to a service.  In fact, don't pass any activity anywhere to store it beyond its normal lifecycle.  There is also almost never any need to create your own thread under most circumstances. It's better to use the tools that Android provides, and follow existing patterns for multithreading as described in Android documentation.


Here is another answer: Instead of using a service, you can use a Fragment with setRetainInstance(true) to do your network call. The fragment will continue to run if the main activity is destroyed due to rotation.
In your activity, in onResume, you would check if the fragment is in the stack (getFragmentManager().findFragmentByTag()). If so, you would register a listener to receive the response to the network call. If it's not there (first time), you instantiate it and register your listener.
In onPause, you deregister your listener.
The fragment will hold the network result until the activity is instantiated and requests the network response.
See this for more details.
Comment for this answer: You instantiate it and give it a tag. Then you use that tag to restore it, as indicated in my answer above.
It is just a fragment, so you would also add an API, like "getResponse" that you would call from onResume if it has completed its task.
Comment for this answer: Thanks for the great info! How do I reference the fragment from the Activity? I assume the onResume method you would save it into an object of my Fragment Class?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:03/03/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><android-activity><service>', 'Title': 'Activity that can access thread on a Service', 'ViewCount': 38, 'id': 35762356, 'user_AboutMe': None, 'user_AccountId': 7946445.0, 'user_DisplayName': 'chewpoclypse', 'user_Reputation': 500.0, 'user_Views': 83.0, 'user_id': 5998820.0}}