HelperActivity extends Activity { @NonNull @WorkerThread public static ActivityResult startActivityForResult(@NonNull Context context, @NonNull Intent intent) { context = context.getApplicationContext(); final ActivityResult result = new ActivityResult(); ResultReceiver receiver = new ResultReceiver(new Handler(Looper.getMainLooper())) { @Override public void onReceiveResult(int resultCode, Bundle resultData) { result.setResult(resultCode, (Intent) resultData.getParcelable(HelperActivity.RESULT_INTENT_EXTRA)); synchronized (result) { result.notify(); } } }; Intent startingIntent = new Intent(context, HelperActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .setPackage(UAirship.getPackageName()) .putExtra(HelperActivity.START_ACTIVITY_INTENT_EXTRA, intent) .putExtra(HelperActivity.RESULT_RECEIVER_EXTRA, receiver); synchronized (result) { context.startActivity(startingIntent); try { result.wait(); } catch (InterruptedException e) { Logger.error(e, "Thread interrupted when waiting for result from activity."); Thread.currentThread().interrupt(); return new ActivityResult(); } } return result; }  @Override final void onCreate(@Nullable Bundle savedInstanceState); @Override void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults); @WorkerThread @NonNull static int[] requestPermissions(@NonNull Context context, @NonNull String... permissions); @NonNull @WorkerThread static ActivityResult startActivityForResult(@NonNull Context context, @NonNull Intent intent); @NonNull
    static final String PERMISSIONS_EXTRA; @NonNull
    static final String RESULT_RECEIVER_EXTRA; @NonNull
    static final String RESULT_INTENT_EXTRA; @NonNull
    static final String START_ACTIVITY_INTENT_EXTRA; }
@Test public void testStartActivityForResult() throws InterruptedException { final Intent activityIntent = new Intent(); final int expectedCode = 1; final Intent expectedData = new Intent(); Thread thread = new Thread(new Runnable() { public void run() { HelperActivity.ActivityResult result = HelperActivity.startActivityForResult(TestApplication.getApplication().getApplicationContext(), activityIntent); Assert.assertEquals("Unexpected result code", expectedCode, result.getResultCode()); Assert.assertEquals("Unexpected result data", expectedData, result.getIntent()); } }); thread.start(); ShadowApplication application = Shadows.shadowOf(RuntimeEnvironment.application); for (int i = 0; i < 10; i++) { Intent intent = application.peekNextStartedActivity(); if (intent != null && intent.getComponent().getClassName().equals(HelperActivity.class.getName())) { break; } Thread.sleep(10); } Intent intent = application.getNextStartedActivity(); Assert.assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK, intent.getFlags()); Assert.assertEquals(intent.getComponent().getClassName(), HelperActivity.class.getName()); Assert.assertEquals(activityIntent, intent.getParcelableExtra(HelperActivity.START_ACTIVITY_INTENT_EXTRA)); ResultReceiver receiver = intent.getParcelableExtra(HelperActivity.RESULT_RECEIVER_EXTRA); assertNotNull(receiver); Bundle bundle = new Bundle(); bundle.putParcelable(HelperActivity.RESULT_INTENT_EXTRA, expectedData); receiver.send(expectedCode, bundle); thread.join(100); Assert.assertFalse("Thread is not finishing", thread.isAlive()); }