Snippets and themes in Android

I have a MainActivity that uses fragments.

OnCreate of MainActivity terminates onCreate with

welcomeFragment = new MyWelcomeFragment(); fr.beginTransaction().replace(R.id.mainContent, welcomeFragment).commit() 

As part of MyWelcomeFragment on onResume, the stream begins to receive updates from my web server. If a user selects an action before the thread completes and switches to MyNewsFragment, what happens to a thread that is not already running in the MyWelcomeFragment thread?

The theme was created using: (myThread and the handler are instance variables)

  myThread = new Thread(new Runnable() { @Override public void run() { sendDataToServer(""); handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { public void run() { onTaskDone(); } }); } }); myThread.start(); 
+5
source share
3 answers

Dalvik saves all references to Thread at runtime so that your thread continues to work if it has not been completed or completed (some link ). Therefore, depending on where you start your stream, you can create more than one. There is a clean way to cancel Thread, in which case you may need to first cancel the HTTP request inside sendDataToServer and use a common flag to stop the thread.

In a wider picture, I would suggest

  • move the network method to Activity and process it there, since it has a longer life than the Fragment
  • Use Android Volley for networking. With it, you can manage unintentional multiple requests to send data to your server. Since each request can be attached using a tag, you can cancel any one with a specific tag in the queue (in your case, the one that corresponds to the sendDataToServer process) before starting a new one.
  • and finally use the Publisher-Subsriber template, which is already available to libraries, such as Otto or EventBus . This allows you to communicate between fragments or actions, avoiding problems associated with the life cycle. At the core: the publisher issues events to subscribers registered on it, and unlike listeners, both the publisher and the subscriber are completely separate. In your case, when sendDataToServer exits, you won’t know if the fragment containing onTaskDone remains. If this method controls the user interface, and the fragment destroyed its representation, you will definitely receive an error message. Thus, onTaskDone must be wrapped inside a subscriber method whose parent fragment is registered in the http event publisher and unregistered as soon as its view is destroyed.
+2
source

It will work until the run () method is executed, which probably lasts a long time when sendDataToServer ("") executes, because the handler must be quite fast compared to network IO - or the flow is interrupted.

Are you still interested in the result if the user switches the fragments?

Do you support the link to the greeting fragment? (Via fragment manager or activity) - if so, you can access the result.

If the user returns to the greeting fragment, the previous stream link will be lost.

+1
source

Thread will continue to work until MyWelcomeFragment , and if you did not kill it in onPause() .

0
source

All Articles