How will UI changes be processed in the onClickListener before starting a new activity in Android?

依然范特西╮ 提交于 2019-12-25 06:38:22

问题


I have been searching here in this forum but I cannot find any answer to my question. I have the following problem:

I have a imageview and two texts, and I want to change the color on selection to show show visual feedback before loading the next activity.

@Override
public void onClick(View v) {
   openFormOverviewButton.setColorFilter(0x22FFFFFF, Mode.SRC_ATOP);
   openFormOverviewTitle.setTextColor(0xFF00851B);
   openFormOverviewText.setTextColor(0xFF00851B);
   Intent tki = new Intent();
   tki.setClass(getApplication(), DataCollectorFormOverviewActivity.class);
   startActivity(tki);
}

I would expect that the button and the texts are changed and then the new activity is started. However, the text does not change and the activity starts. If I invoke the three lines in a runnable on the UI thread (runOnUiThread(new Runnable() {...}) then the changes are applied before the activity is started. This is actually strange, because the onClick method is called on the main thread aka. UI thread.

Does that mean that changes to the UI thread are not executed imediatelly on the UI thread? Or am I doing something completely wrong?

Best, Adam


回答1:


Correct. Changes to UI elements are not executed immediately. They're executed the next time the UI loop runs, which is after you release control. This is why you can't do expensive operations on the UI thread.



来源:https://stackoverflow.com/questions/4717722/how-will-ui-changes-be-processed-in-the-onclicklistener-before-starting-a-new-ac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!