The first time to see my article friends, you can follow, like, forward, every day to share a variety of dry technology and program ape fun

preface

Android 11 (R) is the next generation of Android in 2020. Google released Android 11: Developer Preview 3 earlier this month

On Android 11, Toast’s behavior has changed

  • Prohibit background custom Toast
  • Text Toast does not allow customization
  • SetView () is deprecated
  • Toast.callback Callback is added

Android 11 API changes

Prohibit background custom Toast

Custom Toast “cannot be displayed” while the app is in the background, Instead, the text toast of “Background custom toast blocked for package [packageName] See g.co/dev/toast.” is displayed

Normal text toasts are not affected

Text Toast does not allow customization

The default toast is text toast. If you want to use a custom toast, you call the setView() method

When targetSdkVersion is R or higher, calling the setGravity and setMargin methods will do nothing

Android R, as stated in the official documentation, only affects “text Toast”, custom toasts are not affected

The call is invalid and only affects text toast

Call the setGravity and setMargin methods in test toast, but the toast position is not centered

SetView () is deprecated

The **setView() ** method is marked deprecated

“Deprecated” indicates 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 taking steps to ban custom toasts

Currently ** targetSdkVersion ** for R or higher app prohibits background popup of custom Toast

At the same time, the setView() method will be deprecated. When this method is removed from the source code, the Toast method will be completely eliminated

Of course, an official alternative is available, using Snackbar

Toast.callback Callback is added

A new Callback (toast.callback) was added to tell Toast to show and hide. You can easily add them to Toast by:

val toast = Toast.makeText(this, R.string.simple2_toast, Toast.LENGTH_SHORT)toast.addCallback(object : Toast.Callback() { 

override fun onToastShown() {  
     super.onToastShown()
     Log.d(TAG, "onToastShown") 
}    

override fun onToastHidden() { 
    super.onToastHidden()  
    Log.d(TAG, "onToastHidden") 
}

})
toast.show()

Some tips and demos

Demo here, toggle Flavor to specify a different targetSdkVersion

I had some minor problems writing the demo

tip1

The Handler() no-argument constructor and Handler(handler.callback) constructor are deprecated

This is simply the configuration Looper to display when initializing the Handler

There are bugs with improper use of handlers, such as creating a Handler from a no-argument constructor in a child thread, where you might see exceptions like this

I won’t go into the details here, but this is a must for Android developers. The official way to avoid the problem is to force the Handler constructor passed in to Looper

tip2

In the past, creating a Toast object using the Toast constructor and calling the setText method crashed, not when targetSdkVersion is R.

The same code targetSdkVersion lower version crashes:

Crash, but setting position takes effect:

Abnormal log:

API 29 source code:

In API 29, the setText() method is called to ensure that mNextView is not empty, whereas the setView assignment is called to mNextView

So in the past, using the Toast constructor to create a Toast object could not create a plain text Toast, and the setView method had to be called