简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM