android stop thread - android

hi im wanting to stop all threads when my main activity closes as some are still running afterwards and are giving NullPointerException as they try to access ArrayLists which no longer exist. however none of the obvious methods are working and they are also deprecated. is it possible as im currently using a try/catch statement as a workaround but would prefer a fix.
TIA
ng93

You need to have your MainActivity tell your threads that it's time to end. You could do this with some sort of value that each thread checks before they access the ArrayLists. Or you could live with the try/catch workaround. But there aren't any good, safe ways of killing threads, which is why those methods got deprecated.

Related

how to see FatalExceptions in logcat when using Kotlin

I've just started work with Koltin and my question might be a little strange to someone who have more experience,
but how can i see fatalExceptions in logcat? For example, i have an app that is already developed by another dev,
there is an error in one activity - after pressing the button apps crash and restart to main activity.
I don't see any usefull informations in logcat(in fabric also!), moving on trough whole code from listener to fragment and many classess is very time consuming. There must be some way to figure it out quicker, right?
Exceptions should be shown/thrown in logcat, same as with Java.
If the exception is thown within rxjava or a kotlin coroutine, make sure you have defined an error handler, otherwise the exception might get swallowed.
Then make sure you have selected the right app in logcat and that no filter is active.
Also make sure there is no other global Exception handler defined besides fabric.

Can an Android textView become frozen if two threads try to access the same referenced object at the same time?

Can a thread trying to access the same part of memory at the same time as another thread cause textViews on an android to freeze? if so what is the solution?
The solution is in the design of Android. The method calls to change something in the appearance of a TextView must be called within the Android-UI Thread. You get an exception, if you call methods like TextView.setText() from another thread.
The proposed solution for this is to call Activity.runOnUiThread() or similar and pass your changes to the runnable. See the Android guide about multithreading for more information and the different possiblities.
I never read about View' Thread safety concept in android Documentation. But i guess you will get exception because Changing by two Thread at a time is not good user experience. My conclusion is View are Thread safe and always be updated in UI thread.

Handling program flow for an application

I am currently trying to write a simple application very similar to this:
http://lab.andre-michelle.com/tonematrix
My main problem is that I do not know how to handle the general program flow. Traditionally, I would use a loop inside the main function that handled the drawing and updated the state and everything. The way the android framework works is a bit confusing since the access points into the program are the various onSomething() functions. This is quite confusing for a beginner.
How do I keep track of time and how do I know how when to move to the next square column?
Do I HAVE to use threads? Is there a single thread solution, similar to the single loop approach?
You can use timers, they do already run in their own threads.
You can also use handlers for timed execution.
In case of the sample program u linked to, you would add something with onTouch and have a timer running in the background to periodically play the tune.
The activity lifecycle should be seen as the lifeline of your program not so much of the code in it.
How to keep track of time ?
System.currentTimeMillis();

ANR Exception handling

I have an application, where we are providing the Remote UI(which contains all the buttons to control the Media server).
The problem is when we click any of the button, we are executing the corresponding action, which is very long UPNP network operation.
so when we press the buttons continuously , finally the device comes up with ANR Exception and force close the application. I made some research on this ANR Exception and finally found that, we can use Thread or AsyncTask to solve this problem.
But in my application since we are providing so many buttons, when user presses buttons continuously , it may inturn lead to lot of threads created in the application.
Please give me your suggestions on this.
How to overcome this problem?
Thanks
One of the many advantages of using AsyncTask is that it manages the threading (and thread pooling) for you. So if you use AsyncTask, you shouldn't have the problem of creating too many threads.
In addition, if you're concerned with creating too many AsyncTasks, consider putting the tasks in a member variable (such as a Queue or ArrayList) and keeping track of their state. If one is still processing it might not be necessary to start another. Or you can remove tasks whose results are no longer needed.
Just keeping track of the button click in member variable and using One AsyncTask you can perform this long running operation in queue wise.
If possible just avoid multi-threads for these operations.

Android Concurrency issue

I have strictly separated the layers between different parts of my Android application.
At some point of execution I am updating my data from xml service on Internet. That updating takes up about 10 seconds and is done completely in the background - meaning the user interface of an application works fine. However further calls to my class (later - DataManager) which is responsible for data updating (after update has been started but not yet finished) makes my application crash. NullPointerException is thrown with objects which NEVER are null.
So I assume that only one Thread can use my DataManager at one time and calls to DataManager from other threads ends up in exceptions.
I tried various combinations of putting 'synchronized' keywords near sensitive methods but it seems that when update is executing - NOTHING can use ANYTHING from my DataManager.
BTW other classes which are related to DataManager during the execution also seem to hold null objects.
I guess I am just missing some kind of design pattern which is used to deal with concurrency problems and maybe anyone can suggest me something?
I had trouble dealing with using the Apache http client because of threading issues and I think your issue could be similar in that respect. What I ended up doing was setting up a callback scheme. This may or may not work for you, of course.
This may seem a bit Rube Goldberg-like to you, but it worked for me.
I would have my UI thread call my data manager object with a method that spawned a thread to go and acquire the data. The method's return value is an object that would EVENTUALLY have the data in it. I would have my activity extend an interface, something like "DataCallbackInterface", with a method that the thread would call after it acquired the data (i.e. the last line in run()). Since that call will inherently be within another thread, you'll need to use a Handler to run anything useful in your implementation of the DataCallbackInterface method. When that method is called, you will know for a fact that the data is there and not rely on any strange synchronization flags to get it right.

Categories

Resources