passing data from class to Activity via interface and modifying the UI - java

I have a class that handles Bluetooth communication and this class needs to forward data that it received from Bluetooth to my activity class. I used to pass this data via Broadcasts but now I am trying to pass the data via an interface. The problem I am having is that upon receiving the data in my activity, I can`t make changes in the UI and I am getting an error that says "Only the original thread that created a view hierarchy can touch its views."
//My Activity class implements an interface and receiveDataFromBluetoothConnection is a method in that interface
//My bluetooth service sends data to my activity via this method.
public void receiveDataFromBluetoothConnection(String data) {
processIncomingBtMessage(data);
}
private void processIncomingBtMessage(String incomingMessage) {
//...
//...
//...
if (message == BtMessageIn.BT_MESSAGE_IN_SYSTEM_OFF) {
Log.d(TAG, "remoteControlBubblePillar: bReceiver: Setting Button to On");
arduinoPowerStatus = false;
LightsButtonsBackgroundUnpressed();
btnOnOff.setBackgroundResource(R.drawable.button_shape_round_corners_gradient_green);
btnOnOff.setText(R.string.On);
}
//...
//...
//...
}
Here is the full error message that I get
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7913)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1373)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5449)
at android.view.View.invalidateInternal(View.java:14825)
at android.view.View.invalidate(View.java:14761)
at android.view.View.invalidateDrawable(View.java:19051)
at android.widget.TextView.invalidateDrawable(TextView.java:6353)
at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:436)
at android.graphics.drawable.Drawable.setVisible(Drawable.java:820)
at android.view.View.setBackgroundDrawable(View.java:19522)
at android.support.v7.widget.AppCompatButton.setBackgroundDrawable(AppCompatButton.java:86)
at android.view.View.setBackground(View.java:19498)
at android.view.View.setBackgroundResource(View.java:19481)
at android.support.v7.widget.AppCompatButton.setBackgroundResource(AppCompatButton.java:78)
at com.bubblewall.saik.bubblewall.remoteControlBubblePillar.processIncomingBtMessage(remoteControlBubblePillar.java:354)
at com.bubblewall.saik.bubblewall.remoteControlBubblePillar.receiveDataFromBluetoothConnection(remoteControlBubblePillar.java:275)
at com.bubblewall.saik.bubblewall.BluetoothConnection$1.onCharacteristicChanged(BluetoothConnection.java:97)
at android.bluetooth.BluetoothGatt$1.onNotify(BluetoothGatt.java:400)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:177)
at android.os.Binder.execTransact(Binder.java:573)
Any ideas how can I fix this issue? Or should I just switch back to Broadcasts?

The problem is that your Bluetooth class doesn't run on the main/UI thread, as the activity does. In order to avoid race conditions, Android crashes the app to let you know that you're doing something wrong.
Fortunately, there is a simple way to fix this by using runOnUiThread(Runnable):
public void receiveDataFromBluetoothConnection(String data) {
runOnUiThread(() -> processIncomingBtMessage(data));
}
But I have a better suggestion. Instead of letting the activity handle this, it would be better to design your other class such that it makes sure that when it notifies your activity it does so on the proper thread.
new Handler(Looper.getMainLooper()).post(() -> listener.receiveDataFromBluetoothConnection(data))
It's better to do it this way because in case other activities use the Bluetooth class they don't have to worry about making sure they execute code on the proper thread, the Bluetooth class already handles that.
For a more in-depth explanation on exactly what's happening and what the proper way to handle it is you can read Communicating with the UI Thread, on developer.android.com.

Related

How to handle communication between different viewModels?

I have an activity that contains a number of fragments. Each fragment has a viewModel scoped to its lifetime with some logic inside. The host activity has a viewModel too, including some code to show a popup-style message.
I want my fragment viewModels to be able to post messages to this popup. However, how could I access the activity-viewModel from inside one of my fragment-viewModels?
I'll write some exemplary Kotlin code, but the question is not specific to Kotlin since it's more of an architectural issue.
class MyActivityViewModel {
...
popupMessage = MutableLiveData<String>("") // is observed by my activity
fun postMessage(text: String) {
popupMessage.value = text
}
}
class MyFragmentAViewModel {
...
fun someFunctionA() {
// want to call ActivityViewModel's postMessage from here
}
}
class MyFragmentBViewModel {
...
fun someFunctionB() {
// want to call ActivityViewModel's postMessage from here too
}
}
I can't easily call ViewModelProvider since I'd rather not keep a reference to an Activity in my viewModel. The only direct option I see is to pass the activity-viewModel to the fragment-viewModels through the constructor or an init() method. That should be safe since the parent viewModel's lifetime should exceed the fragment viewModels' lifetime. I think.
Still, that solution rubs me the wrong way.
Are there any other alternatives? Or perhaps an entirely different approach to the issue?
Here's the thought of a greenhorn:
Can't you tell the activity that your fragment wants to use its method?
If you
Make an interface with a method a la "fragmentAWantsToUsePostMessage" in your fragment
Implement the interface in the activity, so that every time fragmentAWantsToUsePostMessage is called, the activity calls postMessage
Get a reference to the implementation of the interface in your fragment
Use that reference when the fragment needs to call "post message"
Shouldn't that work? Or is that against your "not keeping a reference"?
As I said: I'm new to all of this, so I might be completely wrong.
I can see that there's a post on medium that might be relevant: How to Communicate between Fragment and Activity

Best way to return data to MainActivity from AsyncTask

I'm using an ASyncTask in my app to get some data (a short URL) via a REST API from a web service (Bitly).
When the ASyncTask completes I want to pass the result back to my MainActivity.
Getting the data back to the MainActivity is acheievd by using the onPostExecute method of the AsyncTask.
I've read and read and read about how to do this and there seem to be two general approaches.
Originally I was using a 'WeakReference' approach whereby at the start of the AsyncTask class you create a weak reference to your MainActivity as follows:
private class getShortURL extends AsyncTask<String, Void, String> {
private WeakReference<MainActivity> mainActivityWeakReference;
myASyncTask(MainActivity activity) {
mainActivityWeakReference = new WeakReference<>(activity);
}
{etc etc}
With this approach your AsyncTask class sits outside of your MainActivity class and so a lot of things need to be referenced via the weak reference.
This worked fine (except I suspected - possibly incorrectly - that this weak reference may have been the cause of occassional NPEs), but I then found another way of doing things.
This second approach involved moving the ASyncTask class inside of the MainActivity class.
This way I was able to access everything that was accessible in the MainActivity class directly, inlcuding UI elements and methods defined in the MainActivity. It also means that I can access resources such as strings etc and can generate toasts to advise the user what is happening.
In this case the whole of the WeakReference code above can be removed and the AsyncTask class can be made private.
I am also then able to do things like this directly in onPostExecute or to keep this in a method within the MainActivity that I can call directly from onPostExecute:
shorten_progress_bar.setIndeterminate(false);
shorten_progress_bar.setVisibility(View.INVISIBLE);
if (!shortURL.equals("")) {
// Set the link URL to the new short URL
short_link_url.setText(shortURL);
} else {
CommonFuncs.showMessage(getApplicationContext(), getString(R.string.unable_to_shorten_link));
short_link_url.setHint(R.string.unable_to_shorten_link);
}
(note that CommonFuncs.showMessage() is my own wrapper around the toast function to make it easier to call).
BUT, Android Studio then gives a warning that "the AsyncTask class should be static or leaks might occur".
If I make the method static I then get a warning that the method from the MainActivity that I want to call from onPostExecute cannot be called as it is non-static.
If I make that method from MainActivity a static method, then it cannot access string resources and any other methods that are non static - and down the rabbit hole I go!
The same is true, as you would expect, if I just move the code from the method in the MainActivity into the onPostExecute method.
So...
Is having an AsyncTask as a non-static method really a bad thing? (My
app seems to work fine with this warning in AS, but I obviously don't
want to be creating a memory leak in my app.
Is the WeakReference appraoch actually a more correct and safer approach?
If I use the WeakReference approach, how can I create things like toasts which need to be run on the UI thread and access string
resources etc from the MainActivity?
I read somewhere about creating an interface but got a bit lost and couldn't find that again. Also would this not have the same kind of reliance on the MainActivity that a WeakReference does and is that a bad thing?
I'm really looking for best practice guidance on how to get some data back to the MainActivity and the UI thread from an AsyncTask that is safe and doesn't risk memory leaks.
Is having an AsyncTask as a non-static method really a bad thing? (My app seems to work fine with this warning in AS, but I obviously don't want to be creating a memory leak in my app.
Yes, your Views and your Context will leak.
Enough rotations and your app will crash.
Is the WeakReference approach actually a more correct and safer approach?
It's lipstick on a dead pig, WeakReference in this scenario is more-so a hack than a solution, definitely not the correct solution.
What you're looking for is a form of event bus from something that outlives the Activity.
You can use either retained fragments* or Android Architecture Component ViewModel for that.
And you'll probably need to introduce Observer pattern (but not necessarily LiveData).
If I use the WeakReference approach, how can I create things like toasts which need to be run on the UI thread and access string resources etc from the MainActivity?
Don't run that sort of thing in doInBackground().
I'm really looking for best practice guidance on how to get some data back to the MainActivity and the UI thread from an AsyncTask that is safe and doesn't risk memory leaks.
The simplest way to do that would be to use this library (or write something that does the same thing yourself, up to you), put the EventEmitter into a ViewModel, then subscribe/unsubscribe to this EventEmitter inside your Activity.
public class MyViewModel: ViewModel() {
private final EventEmitter<String> testFullUrlReachableEmitter = new EventEmitter<>();
public final EventSource<String> getTestFullUrlReachable() {
return testFullUrlReachableEmitter;
}
public void checkReachable() {
new testFullURLreachable().execute()
}
private class testFullURLreachable extends AsyncTask<Void, Void, String> {
...
#Override
public void onPostExecute(String result) {
testFullUrlReachableEmitter.emit(result);
}
}
}
And in your Activity/Fragment
private MyViewModel viewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
// ...
}
private EventSource.NotificationToken subscription;
#Override
protected void onStart() {
super.onStart();
subscription = viewModel.getTestFullUrlReachable().startListening((result) -> {
// do `onPostExecute` things here
});
}
#Override
protected void onStop() {
super.onStop();
if(subscription != null) {
subscription.stopListening();
subscription = null;
}
}

Can I send a custom event, or call another method, in another class but not wait for the result?

I would like to send a custom event (or call another method) to another class and not wait for the response?
The best way I can explain it is to use the Windows SDK method. It gave you the ability to "post a message to another window" (WM_POSTMESSAGE). The thing about that feature, which is the part I want, is that the message went into the OS's msg queue. So you did not have to wait for the response, your app could continue on.
So is I have a method in class A that is doing something, and at some point it wants to emulate an OnClick event in class B, but not wait for the result as the method still has more to do.
Is this possible?
Sorry for the long winded question.
You can use the LocalBroadcastManager, which I believe resembles the most what you are seeking.
Contrary to what advised in the comments, I do not feel starting a Thread to call a method is a good pattern here.
Have your sender send a signal via the aforementioned component:
final Intent intent = new Intent();
intent.setAction(FILTER_VALUE);
// put your data in intent
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
where
public static final String FILTER_VALUE= "com.you.yourapp.MY_SIGNAL";
public static final IntentFilter SIGNAL_FILTER = new IntentFilter(FILTER_VALUE);
Whoever is meant to receive the signal shall implement a BroadcastReceiver
this.localBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Do what you have to do here... call you method
}
}
If you are using Android's framework classes (Activity, Fragment, Service, ...) you should follow the standard life-cycle pattern for registering and un-register the receiver:
#Override
protected void onResume() {
// Listen if someone sends data
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(this.localBroadcastReceiver, SIGNAL_FILTER);
}
#Override
protected void onPause() {
// I'm going to the background / or being destroyed: no need to listen to anything anymore...
LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(this.localBroadcastReceiver);
}
otherwise registering it when the object is initialized should be the way to go.
I believe this is the messaging system you are looking for.
If you want to run some code that will run in parallel while your current code is running, the easiest way to do this would be using a Thread. A thread once started will run in parallel and will not block your application, thus allowing the code that started the thread to continue executing even while the code in the thread is also executing.
To create and use a thread, use the following code:
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
//put the code that you want to run in parallel here
}
});
thread.start();
You should be warned though that using parallel programming without carefull planning may result in very weird results especially if two different threads are accessing and changing the same variables or data. You should therefore plan your code very carefully if you plan on using multi threading.

Get back to Android activity after background thread is complete

I'm using Parse with my Android app, and my code looks like this:
public class SignupActivity extends Activity {
//Collect signup data
User.signupInBackground(data);
}
public class User{
//User methods, constructors, etc
public static void signup(data){
ParseUser pUser = new ParseUser();
//Build data into pUser
pUser.signUpInBackground(new SignUpCallback() {
public void done(ParseException e){
if (e!=null){
Log.v("Signup",e.toString());
}
}
});
So the question is, how do I notify my activity when the signUpInBackground process is complete? I can't have SignupActivity implement SignUpCallback because it's an abstract class and I have to extend Activity.
Ultimately, what I'm trying to do is display a dialog box or waiting animation, and then get rid of it when the background thread is done. Alternatively, the background thread could launch an activity. The problem with this is that the User class and the anonymous inner class don't have their own Context, so they can't start activities.
I'm fairly new at this, so thanks for your help.
Several approaches might work given your current code structure.
Create a Handler in SignupActivity and pass that to the User so it has a way of interacting with the activity.
Make SignUpCallback an interface instead of an abstract class.
Create an instance of a concreate subclass of SignUpCallback in your SignupActivity class; it will have access to the methods of SignupActivity.
I'm assuming that signUpInBackground is executing on a worker thread and that the callback is invoked from that thread. If that's correct, then in all cases you will need to interact with SignupActivity through a Handler. Thus, I'd suggest method #1 unless the other approaches allow for cleaner code.

In android, how can I separate the data loading into its own thread?

So I'm working on my "hello world" application in android/java, and elected to do a sports app (which is strange...I don't like sports...but whatever). So I set up my layout, allow users to 'drill down', so they can see the layout for Baseball, or MLB, or the Indians. Say a user selects 'Indians' from the MLB view. I update the tabs, potentially the color scheme, background, etc, and load the data for the 'news' and 'players' tabs (the latter of which is unique to team layouts). Unfortunately, api calls can sometimes take relatively long to complete, especially when the free API from ESPN is capped at 1 call per second. I do some significant caching already, but there's no way I can guarantee that I won't be loading both 'news' and 'players' for 'Indians' at the same time, so one of the requests will have to wait a full second to return.
So my solution is to have a data loading thread - the UI says 'get me this data', and does the UI work not contingent on the data being there. The question though is - once the data is returned from the data loader (as each piece comes back), how should it update or notify the UI appropriately? My current thought is:
UI thread:
OnSelectIndians()
{
DataLoadThread.GetIndiansPlayers();
DataLoadThread.GetIndiansNews();
// UI stuff
}
OnPlayersLoaded(Array Players)
{
if (layout == INDIANS_LAYOUT) // Make sure we haven't changed layouts
{
foreach player in Players
tab[PLAYERS].textview.text += player
}
}
But this isn't a problem I've had to deal with before. Is this the right way to go about it? Or is there a better/easier design I can use? I don't particularly like requiring the UI thread to have a 'on data returned' method for every type of data I can request. My other loosely-formed idea is to create a lambda function in the UI code, which is passed to the data loader and executed in the data loading thread, so:
DataLoadThread.Queue(
foreach player in GetIndiansPlayers()
myView.tab[PLAYERS].textview.text += player;
);
But I think this is probably the worse route, as now we have 2 threads interacting with the UI. Any advice?
Edit: Okay I got it working using AsyncTask. Out of the box, it still has the problem listed above that I would have to create a new derived class for every type if data I load (so PlayerLoadTask, NewsLoadTask, StandingsLoadTask, etc etc). I also wanted was to have most of the logic visible during the call, so if I'm looking at the event code I know what its doing. Below is the working implementation - would appreciate any feedback on it, but I'll accept the first answer below just the same.
abstract public class LoadDataHelper {
public LoadDataHelper(DataLoader dl, Object param) {
mDataLoader = dl;
mParam = param;
}
abstract public LinkedList<String> LoadData();
protected DataLoader mDataLoader;
protected Object mParam;
}
abstract public class UpdateUIHelper {
public UpdateUIHelper(MyActivity context) {
mContext = context;
}
abstract public void UpdateUI(LinkedList<String> results);
protected MyActivity mContext;
}
private class LoadDataTask extends AsyncTask<Void, Void, LinkedList<String> > {
private LoadDataHelper mLdh;
private UpdateUIHelper mUih;
LoadDataTask(LoadDataHelper ldh, UpdateUIHelper uih) {
mLdh = ldh;
mUih = uih;
}
#Override
protected LinkedList<String> doInBackground(Void... params) {
return mLdh.LoadData();
}
#Override
protected void onPostExecute(LinkedList<String> results) {
mUih.UpdateUI(results);
}
}
//
// .....
//
LoadDataTask task = new LoadDataTask(new LoadDataHelper(mDataLoader, "football") {
public LinkedList<String> LoadData() {
return mDataLoader.LoadLeaguesFromSport((String)mParam);
}
},
new UpdateUIHelper(this) {
public void UpdateUI(LinkedList<String> results) {
TextView tv = (TextView)findViewById(R.id.tv1);
tv.setText("");
for (String res : results) {
tv.append(res + "\n");
}
}
});
task.execute();
Take a look at:
1) AsyncTask
http://developer.android.com/reference/android/os/AsyncTask.html
The AsyncTask.onPostExecute will be executed in the UI thread.
I think this is the most common technique to do background processing.
2) runOnUIThread: If you are managing your own worker thread, you can use this in a worker thread to make sure code is run on the UI thread.
Its always better to have UI work in UI thread, and Non-UI work in Non-UI thread, But this became a Law from the arrival of HoneyComb in Android.
2 ways to do it in Android.
1. Use Java thread with Handler..
Create a thread to do the process heavy background task, and then display the data using
Handler...
2. Use AsyncTask<>, which is specially designed for Android, to sync the UI work and Non-UI
work. AsyncTask is also known as painless threading.
This is what AsyncTask was designed to do ,
Here is the tutorial that I learned from.
Here is what you do
Create a class that extends AsyncTask
Implement doInBackground and onPostUpdate methods
In the onPostUpdate method update the ui , you can use runOnUiThread to avoid any issues during ui update
The advantage of this using Async tasks is that you can even update the progress and display a visual indicator to the user , You can as easily cancel the task to stop the loading if required
AsyncTask is essentially a helper that simplifies the use of threads
Your best bet is to use the AsyncTask. I created a similar app that made 25+ calls to a server to download images. Using the AsyncTask will cut that time greatly, and still provide a great user experience. Here is a great tutorial on how to use/setup an AsyncTask:
http://www.peachpit.com/articles/article.aspx?p=1823692&seqNum=3
async task is the solution for you
here is a tutorial

Categories

Resources