Toast
Restrictions
There are a number of new limitations on Toast
that you will need to take into account.
The big one is displaying a Toast
with a custom View
(via setView()
). This is still allowed, but only from the foreground. If you try this from the background, the Toast
is not displayed and a warning about the violation is recorded in Logcat.
For apps with a targetSdkVersion
of 30 or higher, you also:
- Cannot call
getView()
to get theView
displayed by theToast
(it just returnsnull
) - Cannot modify the margins or gravity
- Cannot retrieve the margins or gravity (return values from methods like
getGravity()
“don’t reflect the actual values, so you shouldn’t rely on them in your app”)
The rationale for all of these is to prevent the user from using a Toast
to occlude critical aspects of the UI. For example, in principle, an app could display a custom Toast
over top of a system permissions dialog. The Toast
could show a message that implies that the permission being requested is more benign than it really is. While this attack is unreliable, given the short and transient nature of a Toast
, it could still work.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.