0

This is likely an issue with me not properly understanding threading, however I often try and group code into a seperate class and become hindered by some it not being on the UI thread. I can't find anything that properly explains this however. As an example:

public class MyActivity extends Activity {
 @Override
 public void onCreate (Bundle bundle) {
  HelperClass = new HelperClass(context); 
 }
}

public class HelperClass extends ContextWrapper {  
 someMethod () {
   // Do stuff
 } 
}

When someMethod is called, if it's trying to do something thread dependant, I will get an error. I've had this with trying to keep some WebView logic in a seperate class as well as trying to access a realm database.

Using runnables seems very messy and I clearly don't understand exactly what's happening to beable to properly structure my code.

Can anyone explain why? and what the best soloution is?

Honest Objections
  • 773
  • 1
  • 6
  • 13
  • There are tons and tons of links. [here](https://stackoverflow.com/questions/14053959/how-does-multi-threading-work-in-android) or [there](https://developer.android.com/guide/components/processes-and-threads.html) or just https://www.google.co.uk/search?q=android+multi+threading&oq=android+multi+threading#q=android+multithreading ... what exactly did you not understand when doing research? – GhostCat Jun 28 '17 at 09:22
  • Because I'm not _trying_ to run the second class in a seperate thread, it just is. I've looked at the android threads and proccesses docs but there's no mention of things being automatically threaded. Why is the Activity on the UI but the class instantiated within it not? – Honest Objections Jun 28 '17 at 09:24
  • Get the call stack and see from there your method is called. Could be an AsyncTask if you are not using explicit threads. – sstn Jun 28 '17 at 09:28
  • HandlerThread is the lowest call, however as I'm not especially familiar with Threads, I'm not really sure where to start. Should I be structuring non-activity classes differently or.. I'm just generally unsure if I'm using the correct layout if this is an issue tha tkeeps coming up. – Honest Objections Jun 28 '17 at 09:39
  • This certainly isn't a "minimal verifiable **complete** example". – EpicPandaForce Jun 28 '17 at 12:07

0 Answers0