preface
Android 11 (R) is the next generation of Android for 2020. Google released Android 11: Developer Preview 3 last week
Toast’s behavior has changed in Android 11
-
Do not customize Toast in background
-
Text toast customization is not allowed
-
SetView () is deprecated
-
New toast.callback Callback
Android 11 API changes
Do not customize Toast in background
Custom Toast “cannot” be displayed when the app is in the background, “Background custom toast blocked for package [packageName] See g.zip /dev/toast.” is displayed instead
Normal text toast is not affected
Text toast customization is not allowed
The default toast is text toast. To use a custom toast, you need to call the setView() method
When the targetSdkVersion is R or higher, the calls to the setGravity and setMargin methods do nothing
❝
Android R, as stated in the official documentation, only affects “text Toast”. Custom Toast is not affected
❞
The setGravity and setMargin methods are called in the text toast, but the toast is not centered
SetView () is deprecated
The setView() method is deprecated
❝
“Deprecated” means that the feature is still available, but may be removed in future Android releases. Developers are advised to avoid long-term use of this feature
❞
As you can see, officials are banning custom Toast step by step
Currently, apps with targetSdkVersion R or higher prohibit background pop-ups from defining toasts
Also, the setView() method tag is deprecated. When this method is removed from the source code, the custom Toast method will be eliminated completely
❝
Of course, the official alternative is Snackbar
❞
New toast.callback Callback
A new Callback (toast.callback) has been added to notify Toast to show and hide. You can easily add it to the Toast by:
val toast = Toast.makeText(this, R.string.simple2_toast, Toast.LENGTH_SHORT)
toast.addCallback(object : Toast.Callback() {
override fun onToastShown(a) {
super.onToastShown()
Log.d(TAG, "onToastShown")
}
override fun onToastHidden(a) {
super.onToastHidden()
Log.d(TAG, "onToastHidden")
}
})
toast.show()
Copy the code
Some tips and demo
Demo is here. You can specify different TargetsDkVersions by switching Flavor
I had a few problems writing the demo
tip1
The Handler() no-parameter constructor and the Handler(handler.callback) constructor are deprecated
Simply put, this is the configuration Looper to display when initializing Handler
There is a bug with Handler misuse, such as when a child thread creates a Handler with a constructor that takes no arguments. You might see this exception
I won’t go into the details here, but this is essential knowledge for Android developers
Officials avoid problems by forcing the Handler constructor passed in to Looper
tip2
Using the Toast constructor to create a Toast object and call the setText method used to crash, but not when targetSdkVersion was R
API 29 calls the setText() method to ensure that the mNextView, which is assigned by the setView call, is not empty
Therefore, using the Toast constructor to create Toast objects in the past will not create a normal text Toast. You must call the setView method
As for API 30, there was definitely a change here, and since I can’t see the source code, I can’t guess what the official intention is
If you have any ideas, please leave them in the comments section
About me
I am a Fly_with24
-
The Denver nuggets
-
Jane’s book
-
Github