Fixed race condition in DialerUiTaskFragment.

This was manifesting as a flaky test:

https://sponge.corp.google.com/target?id=935f070f-e23e-4a67-b9cb-42e0e6f97c88&target=//third_party/java_src/android_app/dialer/javatests/com/android/espresso/dialer/main/impl:MainActivityIntegrationTest

Test: unit
PiperOrigin-RevId: 166873503
Change-Id: I00fcdbc1ce91cfcf984ffda09d538a5e1093368f
diff --git a/java/com/android/dialer/common/concurrent/DialerUiTaskFragment.java b/java/com/android/dialer/common/concurrent/DialerUiTaskFragment.java
index b6068b2..f0b7537 100644
--- a/java/com/android/dialer/common/concurrent/DialerUiTaskFragment.java
+++ b/java/com/android/dialer/common/concurrent/DialerUiTaskFragment.java
@@ -146,14 +146,38 @@
       if (successListener == null) {
         LogUtil.i("DialerUiTaskFragment.runTask", "task succeeded but UI is dead");
       } else {
-        ThreadUtil.postOnUiThread(() -> successListener.onSuccess(output));
+        ThreadUtil.postOnUiThread(
+            () -> {
+              // Even though there is a null check above, it is possible for the activity/fragment
+              // to be finished between the time the runnable is posted and the time it executes. Do
+              // an additional check here.
+              if (successListener == null) {
+                LogUtil.i(
+                    "DialerUiTaskFragment.runTask",
+                    "task succeeded but UI died after success runnable posted");
+              } else {
+                successListener.onSuccess(output);
+              }
+            });
       }
     } catch (Throwable throwable) {
       LogUtil.e("DialerUiTaskFragment.runTask", "task failed", throwable);
       if (failureListener == null) {
         LogUtil.i("DialerUiTaskFragment.runTask", "task failed but UI is dead");
       } else {
-        ThreadUtil.postOnUiThread(() -> failureListener.onFailure(throwable));
+        ThreadUtil.postOnUiThread(
+            () -> {
+              // Even though there is a null check above, it is possible for the activity/fragment
+              // to be finished between the time the runnable is posted and the time it executes. Do
+              // an additional check here.
+              if (failureListener == null) {
+                LogUtil.i(
+                    "DialerUiTaskFragment.runTask",
+                    "task failed but UI died after failure runnable posted");
+              } else {
+                failureListener.onFailure(throwable);
+              }
+            });
       }
     }
   }