Angus
Angus

Reputation: 12621

runOnMainThread vs Handler in android - when to use which one for the mentioned scenario

I have implemented a coachmark in onCreateOptionsMenu method in an activity.

/* Coachmark - preview */
        if (preview.isEnabled(uuserID)) {
            new runOnMainThread(() -> {
                    final View anchorView = findViewById(R.id.action_preview);
                    if (anchorView != null) {
                        new CoachMark.Builder(PreviewActivity.this, anchorView, CoachMarkType.CoachMarkDirection.TOP)
                                .setText(R.string.hello_tooltip)
                                .setTextStyleId(R.style.AppStatusBarTextStyle)
                                .setRadius(MiscUtils.dpToPixel(PreviewActivity.this, 4))
                                .dismissOnTouch(true)
                                .dismissOnTouchOutside(true)
                                .build()
                                .show();
                    }
            });
        }

public static Task<Void> runOnMainThread(@NonNull Runnable runnable) {
    return runOnExecutor(runnable, Task.UI_THREAD_EXECUTOR, null);
}

When the activity is in focus, it loads a file and also creates the options Menu for the activity. I was invoking the coachmark from the createOptionsMenu and the coachmark is not getting displayed when I run the thread using the runOnMainThread() and is getting displayed when using Handler().post(). Any reasons why and when to use the handler and when to use the runOnMainThread().

Upvotes: 0

Views: 585

Answers (0)

Related Questions