Android Build a Weather App Hooking Up the Model to the View Weather Icon

Santiago Serrano
Santiago Serrano
2,754 Points

Setting the drawable to iconImageView in the background thread doesn't crash my app

Hello everyone! I tried replicating the app crash caused by touching the view in the background thread on my emulator, but surprisingly, the app doesn't crash, and the correct icon is displayed.

It may be worth noting that I made a small change in the code inside the if(response.isSuccessful()){} block. I thought having a "displayWeather" object was unnecessary since I could just put the "currentWeather" object inside the binding.setWeather() method. Here's my code in there:

if (response.isSuccessful()) {
      currentWeather = getCurrentDetails(jsonData);
      binding.setWeather(currentWeather);

      Drawable drawable = getResources().getDrawable(currentWeather.getIconID());
      iconImageView.setImageDrawable(drawable);

Anyways, that doesn't detract from the fact that I'm still modifying a view made in the main thread in the background thread. Another thing that might be worth noting is that I'm using Android Studio 3.2.1

Santiago Serrano
Santiago Serrano
2,754 Points

Also, when using the runOnUiThread() method, it doesn't require me to make "currentWeather" final.