July 2018
Intermediate to advanced
266 pages
7h 11m
English
The first approach is quite simple. We can create a loadNews() function that directly invokes fetchRssHeadlines() and displays its results in the same way we did before:
private fun loadNews(){ val headlines = fetchRssHeadlines() val newsCount = findViewById<TextView>(R.id.newsCount) launch(UI) { newsCount.text = "Found ${headlines.size} News" }}
Notice that this function is synchronous, and will call fetchRssHeadlines() in the same thread that it's called. Consider this code, for example:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) loadNews()}
Doing this will result in a NetworkOnMainThreadException ...
Read now
Unlock full access