|
Don't use Async tasks. Use literally any other form of threading, preferably Rx or something robust and not-leaky.
|
# ? Jul 19, 2017 01:25 |
|
|
# ? May 29, 2024 20:23 |
|
Volmarias posted:Without looking at or caring about any of your code, I'd like to point out that you can start a new thread from an Activity, it doesn't actually need to live in a service unless you'd like it to persist when the Activity goes away for some reason. So I've done this and as it turns out sending messages to the UI every 1/100th of a second will crash the UI thread no matter what I do. The listening thread is running fine and it processes all of the data thrown at it just how i'd like. I've been trying to figure out a way to update the UI every 1 second (since this is just a monitoring app to make sure that a vehicle is sending the correct messages and we're not testing for timing) I tried using a while loop when ToggleButton.isChecked = true to call the method in the MainActivity that fetches mListenerService.getParsedMessage and updates the TextView but that obviously locked the UI thread due to the while loop constantly looping while the button isChecked, then I tried putting the while loop in its own thread so that I can sleep it for a second but surprise surprise that didnt work either since now I can't call a method that works on the UI thread from a different thread. I've been messing with Handlers for a little bit but it still crashes the UI thread Java code:
How should I got about updating the UI without crashing it? Instead of trying to call the method that works on the UI thread, should set a global String to the value of the mListenerService.getParsedMessage and update the objects from that? I feel like I'm on the cusp of fully understanding this after reading about all this stuff last night when I got home Edit: better way to phrase it: how can I call a method that works on the UI less often than however quickly it's currently being called FAT32 SHAMER fucked around with this message at 23:12 on Jul 19, 2017 |
# ? Jul 19, 2017 23:01 |
|
Posting to the UI thread every 10ms won't cause a crash. You're doing something else wrong. Post the stack trace and relevant code along with line numbers. In case you're trying to update the UI from the background thread, don't. That's what the Handler is for. You post a message to the handler, and the handler's method that you override (you read the docs right?) will run on the UI thread. Edit: it's possible that your device is some sort of uniquely bad car console turd that is programmed to crash if you update too frequently but that would be someone going out of their way to make something bad. I wouldn't put it past an OEM but I would be shocked. Volmarias fucked around with this message at 23:46 on Jul 19, 2017 |
# ? Jul 19, 2017 23:39 |
|
Volmarias posted:Posting to the UI thread every 10ms won't cause a crash. You're doing something else wrong. Post the stack trace and relevant code along with line numbers. There's no stack trace, the UI simply stops responding then five seconds later I get the "your app is unresponsive" dialog box MainActivity Java code:
ListenerService. Java code:
|
# ? Jul 19, 2017 23:48 |
|
You have a while loop in your listener. Why? You've diagnosed the issue. The problem is that you need to return from the listener call so that the rest of the work on the UI thread, like the drawing, can happen. Using a service overcomplicates this for no tangible gain. Get rid of it, just use a background thread.
|
# ? Jul 20, 2017 00:12 |
|
You want something like thisJava code:
How you want to throttle it honestly depends on what the hell you're doing. I'm guessing actually sleeping the thread is bad because you need to be constantly watching for messages coming in, never missing any. I don't know if you'll actually overflow the message queue doing this (?), but it's probably a safer option to add some kind of throttling policy just in case you get a deluge of UDP packets for some reason. The message pool only has 50 message objects by default too, I think, and the system uses them too, which might be why you're crashing But what do you do if you're producing messages faster than you consume them? How do you prioritise them, how long does an OH poo poo message stay around for, are there any others that would override it? Do you drop the original message or show it later? Honestly I'd be tempted to get a basic polling thing going on in your activity like this Java code:
I mean it's buggy (stop and start quick enough and you'll add another runnable to the polling queue, the first won't have seen the flag going false) and you'll have to worry about atomicity if you start doing complicated stuff with the data you're providing, but it's all fixable if you care enough. Just trying to give an example here This might help too I typed bolleazn while writing this, I think it's my best typo yet
|
# ? Jul 20, 2017 00:51 |
|
Volmarias posted:You have a while loop in your listener. Why? Even after you get that off the main thread, why are you closing and re-opening the socket every time you get a message? Just leave it open and keep reading infinitely. It'll block the listener thread while waiting for data to come in, so when you're done close the socket from outside that thread. Ignore the resulting IOException, because you'll get one from closing the socket out from under a blocking receive call.
|
# ? Jul 20, 2017 04:49 |
|
Volmarias posted:You have a while loop in your listener. Why? Volmarias posted:You've diagnosed the issue. The problem is that you need to return from the listener call so that the rest of the work on the UI thread, like the drawing, can happen. I'm gonna mess around with this idea and do a runnable class since making it a service seems to also make it a pain in the dick kitten smoothie posted:Even after you get that off the main thread, why are you closing and re-opening the socket every time you get a message? Just leave it open and keep reading infinitely. It'll block the listener thread while waiting for data to come in, so when you're done close the socket from outside that thread. Ignore the resulting IOException, because you'll get one from closing the socket out from under a blocking receive call. baka kaba posted:You want something like this
|
# ? Jul 20, 2017 12:11 |
|
So idk how terrible this is, but I figured out a stupidly simple way to redraw the textviews and imageview all the time and it works extremely well. I simply used Animation's applyTransformation. I've had the app running for about an hour with no issue, so fingers crossed it is good to go. edit: here's the code Java code:
FAT32 SHAMER fucked around with this message at 18:33 on Jul 20, 2017 |
# ? Jul 20, 2017 18:30 |
|
It's not like you need to apply some sort of trick to make UI things update, just post things on the UI thread. You're creating some sort of bizarre horror, and I hope you at least comment this callback with "// I don't actually understand how to update the UI, this callback isn't necessary" If you're really having trouble putting things on the UI thread, use Activity.runOnUiThread with a Runnable of things to do in the UI thread (not networking, don't make it a loop) Like, would you like to just cheat and have us write this for you? Volmarias fucked around with this message at 20:41 on Jul 20, 2017 |
# ? Jul 20, 2017 20:38 |
|
Volmarias posted:
nonono it's all good, I realized that while this works it's a terrible way to do it and I implemented the handler as was shown by baka. I have much to learn
|
# ? Jul 20, 2017 21:24 |
|
FAT32 SHAMER posted:Where is it opening and closing the socket? I thought once the thread was running, as long as the socket != null it would stay open, unless calling the public method from the service opens and closes it every time... Your listen thread is a while loop. At the top of the loop it opens a socket and blocks until a message comes in. When a message comes in, it dispatches the data received, then you close the socket. Back to the top and repeat. You're closing and reopening for every message. The socket != null check in that loop is kind of useless where it is, because if the socket were null it would've NPE'd earlier on when you called receive on it.
|
# ? Jul 20, 2017 21:44 |
|
Honestly after the initial about doing it through animations, I realised you're basically updating views on a tick and one of them is animating a fade anyway. So you're really polling for data when the animation has faded out, so you can switch the content while it's invisible. From a purely informational UI standpoint I don't think that's too bad? It's basically a self-updating display that's pulling data when it's convenient, based on the animation state. It'd be more awkward to coordinate that if you're pushing data instead
|
# ? Jul 20, 2017 21:57 |
|
kitten smoothie posted:Your listen thread is a while loop. I'm not sure why i didnt recognize this but that explains a few questions i had in the back of my mind baka kaba posted:Honestly after the initial about doing it through animations, I realised you're basically updating views on a tick and one of them is animating a fade anyway. So you're really polling for data when the animation has faded out, so you can switch the content while it's invisible. This was my initial thinking as well. I'm not sure how terrible it is either but i'd rather do something correctly and learn it so I know for the future instead of do a potentially insane hack to make it work Do you guys have any resources with projects I can work through in my spare time? I got about halfway through the big nerd ranch android book i bought about two years ago and while it was insightful, I wasnt sure how useful some of the stuff would be due to a lot of deprecated things (like apparently SoundPool)
|
# ? Jul 20, 2017 22:53 |
|
FAT32 SHAMER posted:Do you guys have any resources with projects I can work through in my spare time? I got about halfway through the big nerd ranch android book i bought about two years ago and while it was insightful, I wasnt sure how useful some of the stuff would be due to a lot of deprecated things (like apparently SoundPool) The BNR book is still probably one of the best resources for "I can write some Java but need to understand the Android framework." They put out a new edition in January so I'd imagine the outdated stuff is gone now (for instance it looks like the only thing deprecated about SoundPool is the constructor method, not the framework class itself). I'd also start looking through some of Dan Lew's RxJava posts (start from the very beginning) so you can avoid ever touching an AsyncTask ever again.
|
# ? Jul 21, 2017 02:43 |
|
kitten smoothie posted:The BNR book is still probably one of the best resources for "I can write some Java but need to understand the Android framework." They put out a new edition in January so I'd imagine the outdated stuff is gone now (for instance it looks like the only thing deprecated about SoundPool is the constructor method, not the framework class itself). Nice! Thanks for this, and thanks for all the help and insight you guys have given me in the last two weeks. Pray for me because i'm the only person on my team familiar with Java and Android (they all did C# in school and I did Java datastructures/android apps and iOS apps)
|
# ? Jul 21, 2017 15:33 |
|
Java is pretty similar to C# (I think of C# as Java With Hindsight), so transitioning isn't terrible. The official Android guides and tutorials aren't terrible anymore.
|
# ? Jul 21, 2017 19:43 |
|
Volmarias posted:Java is pretty similar to C# (I think of C# as Java With Hindsight), so transitioning isn't terrible. That's what I told them. I guess they'll learn it if they absolutely have to, but since we work in automotive their big focus is C/C++ and C#
|
# ? Jul 21, 2017 23:01 |
|
FAT32 SHAMER posted:Nice! Thanks for this, and thanks for all the help and insight you guys have given me in the last two weeks. Pray for me because i'm the only person on my team familiar with Java and Android (they all did C# in school and I did Java datastructures/android apps and iOS apps) Definitely frequent places like this too. You learn a lot by osmosis if you stay in communities of developers. It's but r/androiddev is extremely good as well. I've learned a ton from there.
|
# ? Jul 22, 2017 07:18 |
|
Also community conferences like Droidcon events, or at least watching the videos after the fact.
|
# ? Jul 24, 2017 23:02 |
|
Ok guys I have a super dumb question (again). The last time I built a [complicated multi-activity] app, Fragments didn't exist and everything was straight forward. I've built a NavDrawer based on this tutorial that i found that isn't garbage and it works great A++. Now my question is, do i build my UI in fragment_layouts for each fragment or do I have to do some kind of interface so that the fragment "becomes", for lack of a better word, an activity. For instance, here is the drawer that I've built. (i'm fixing the padding at the top pls ignore) If I click on "Completed", it crashes if it extends AppCompatActivity but it is fine when it extends Fragment, which I guess makes sense. The tutorial has my MainActivity extending AppCompatActivity, however based on the android developer I should be extending FragmentActivity. Should I be extending both? Should I do some kind of fuckery to make the navdrawer launch activities instead of fragments? Thanks
|
# ? Aug 22, 2017 17:25 |
|
AppCompatActivity inherits from FragmentActivity - use the AppCompat version because it basically gives you a lot of the modern look and other stuff in older versions of Android. If you want to launch an Activity you do it the usual way Java code:
The way you're meant to use Activities and Fragments is you have an Activity, and you inflate a layout in it as usual. That layout can include Fragments in the XML, or you can include a container like a FrameLayout with an id you can find, and stick the fragment in there programmatically. If you do it that way you can swap the fragments in and out without having to go to a new Activity So a fragment is like a component you can add to the activity as and when you need it, and maybe reuse it in multiple places. It can have its own layout (so it provides part of the UI) or it can just be a 'headless' fragment that has some useful functionality but doesn't appear visually. What you'll generally do is have an Activity with some main UI (like the toolbar at the top), define your fragments separately, and then add them into the Activity to complete your UI. So you can have a fragment that mostly (or completely) fills the whole Activity's view, yeah! Read this basically http://guides.codepath.com/android/Creating-and-Using-Fragments Also some people hate fragments and say you should never use them, yolo
|
# ? Aug 22, 2017 18:33 |
|
baka kaba posted:AppCompatActivity inherits from FragmentActivity - use the AppCompat version because it basically gives you a lot of the modern look and other stuff in older versions of Android. Awesome, thanks a lot
|
# ? Aug 22, 2017 18:35 |
|
Has anyone done much with ConstraintLayout and used the beta? Does it work better? I've been trying to use the stable version with the layout editor and holy crap it's maddening sometimes. Stuff randomly flying around, creating a chain makes an unselected object vanish into zero height, padding values changing and things acting like they don't have controls. Sometimes I'm scared to touch anything in case it suddenly goes wacky
|
# ? Aug 22, 2017 18:42 |
|
baka kaba posted:Also some people hate fragments and say you should never use them, yolo It's me, I'm that guy. Fragments suck and you should never use them. baka kaba posted:
I have. ConstraintLayout is super awesome but the editor is still extremely buggy and frustrating to use. At best I use it to throw together a few basics, then I use XML for everything else.
|
# ? Aug 22, 2017 19:09 |
|
Taffer posted:It's me, I'm that guy. Fragments suck and you should never use them. Samesies. I guess if you get off on dealing with bugs from multiple layers of lifecycles they're cool.
|
# ? Aug 22, 2017 19:36 |
|
^^^ lol literally just fixed a bug recently by enjoying the [fixed]isAdded[fixed] method on a fragment. That method is my buddyTaffer posted:It's me, I'm that guy. Fragments suck and you should never use them. *no quilted toilet paper, bart punches a wall* Well I mean it can't be worse, at least? it's fine and great when it works, if it can just hold it together to let me build a simple layout then that'll do I'm one of those people who XMLs everything because it's easier and neater to just define it, but all the constraint stuff really benefits from a GUI, until it doesn't of course
|
# ? Aug 22, 2017 20:04 |
|
kitten smoothie posted:Samesies. I guess if you get off on dealing with bugs from multiple layers of lifecycles they're cool. ok holy poo poo am i glad I just finished converting my fragments to activities
|
# ? Aug 22, 2017 21:34 |
|
Taffer posted:ConstraintLayout is super awesome but the editor is still extremely buggy and frustrating to use. Also, yes, gently caress Fragments. I kind of want to like them but in reality it's just a massive lifecycle mess and the real benefits are pretty niche (easier transition animations is probably the main one).
|
# ? Aug 22, 2017 23:08 |
|
I think there'd be a lot fewer fragments in the world if the docs were more clear that a ViewPager doesn't require Fragments (they didn't call it a ViewPager for their health). But every example implementation delivers a pile of fragments so that's what everyone does.
kitten smoothie fucked around with this message at 05:09 on Aug 23, 2017 |
# ? Aug 23, 2017 05:07 |
|
My only complaint with activities over fragments is that the activity comes from the bottom of the screen and (as far as I can tell) I can't make the drawer highlight + make the current activity unclickable/not destroy+create itself when clicked w/e though I'm not about to spend three weeks figuring out UI bullshit when I have a fuckload of stuff I have to figure out on the backend
|
# ? Aug 23, 2017 05:31 |
|
FAT32 SHAMER posted:My only complaint with activities over fragments is that the activity comes from the bottom of the screen and (as far as I can tell) I can't make the drawer highlight + make the current activity unclickable/not destroy+create itself when clicked I have tried but simply cannot figure out what you mean by this.
|
# ? Aug 23, 2017 05:57 |
|
Volmarias posted:I have tried but simply cannot figure out what you mean by this. I'll record it tomorrow
|
# ? Aug 23, 2017 06:03 |
|
FAT32 SHAMER posted:My only complaint with activities over fragments is that the activity comes from the bottom of the screen and (as far as I can tell) I can't make the drawer highlight + make the current activity unclickable/not destroy+create itself when clicked You just need to set up the menu for each activity, once the navigation drawer is initialised you can do getMenu() to poke around in there and find the item you want to change. You probably just want to look it up by id and then do setEnabled() to false. Usually you'd do something like setItemTextColor() to define what things should look like in states like disabled (or you can set that in XML) Here's some people talking about old activity transitions - you can basically either set it on the activity when it starts and do some lifecycle janitoring, or do it app-wide by defining an animation in your app theme
|
# ? Aug 23, 2017 12:29 |
|
baka kaba posted:You just need to set up the menu for each activity, once the navigation drawer is initialised you can do getMenu() to poke around in there and find the item you want to change. You probably just want to look it up by id and then do setEnabled() to false. Usually you'd do something like setItemTextColor() to define what things should look like in states like disabled (or you can set that in XML) Ahhhhh ok derp. Here's a gif of what i was talking about https://giant.gfycat.com/AnnualColorlessKakarikis.gifv edit: ok the world of animations is fun and exciting and the normies are going to love these FAT32 SHAMER fucked around with this message at 15:41 on Aug 23, 2017 |
# ? Aug 23, 2017 15:35 |
|
Looks like I forgot to actually link the stack overflow page but sounds like you found them anyway!
|
# ? Aug 23, 2017 15:47 |
|
baka kaba posted:Looks like I forgot to actually link the stack overflow page but sounds like you found them anyway! Yeah, cheers
|
# ? Aug 24, 2017 04:08 |
|
Unrelated to things in this thread but what software do you use to create such a nice gif of your screen? edit: oh never mind, you were filming your screen. I didn't notice it on my very cropped browser windows. John F Bennett fucked around with this message at 10:59 on Aug 24, 2017 |
# ? Aug 24, 2017 10:48 |
|
John F Bennett posted:Unrelated to things in this thread but what software do you use to create such a nice gif of your screen? haha yeah, i built a new computer at work and havent installed all my software and didn't realize it until i went to record my screen
|
# ? Aug 24, 2017 12:09 |
|
|
# ? May 29, 2024 20:23 |
|
Completely unrelated, is UnboundID the best LDAP lib for connecting my app to my company's LDAP server? I checked out something called Auth0 but they seem to be offering to host a new LDAP server instead of just giving me APIs to use to handle auth stuff
|
# ? Aug 24, 2017 16:46 |