惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
IT之家
IT之家
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
小众软件
小众软件
博客园 - 叶小钗
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
罗磊的独立博客
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
Android: Always show toasts on the background thread
Ashish Bhatia · 2023-10-22 · via ashishb.net

I used to call Toast#show() on the UI thread. Then one day, I received notifications related to an ANR (Application Not Responding) error in one of my Android apps.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
at android.os.BinderProxy.transactNative(BinderProxy.java)
at android.os.BinderProxy.transact(BinderProxy.java:605)
at android.view.accessibility.IAccessibilityManager$Stub$Proxy.addClient(IAccessibilityManager.java:1207)
at android.view.accessibility.AccessibilityManager.tryConnectToServiceLocked(AccessibilityManager.java:1804)
at android.view.accessibility.AccessibilityManager.(AccessibilityManager.java:606)
at android.widget.ToastPresenter.(ToastPresenter.java:138)
at android.widget.Toast$TN.(Toast.java:1151)
at android.widget.Toast.(Toast.java:259)
at android.widget.Toast.makeText(Toast.java:891)
at android.widget.Toast.makeText(Toast.java:879)
at net.ashishb.androidmusicplayer.util.UiHelper.showToast(UiHelper.java:111)

Programmers on StackOverflow are confused about whether it is OK to call Toasts on the background threads. Just like me, they think that Toast must be displayed from the UI thread. Even though there is no reason.

As a primer, anything that touches your app’s UI must be accessed via the same UI thread. And that’s because the Android UI toolkit is thread-unsafe by design. Making it thread-safe would have introduced thread locks and similar efficiency issues.

However, Toasts are not part of your app’s UI. The toasts are displayed as a part of the System UI. So, avoid ANRs by calling them on the background thread.