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


  1. Do not customize Toast in background

  2. Text toast customization is not allowed

  3. SetView () is deprecated

  4. 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

Do not customize Toast in background

Normal text toast is not affected

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

Invalid call affects only text toast
Invalid call affects only text toast

The setGravity and setMargin methods are called in the text toast, but the toast is not centered

Call the setGravity and setMargin methods in the text toast
It’s not centered. It doesn’t work

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

SetView was abandoned

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

Switch the Flavor

I had a few problems writing the demo

tip1

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

No-parameter constructors 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

The error log
Throw exception source

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

The same code targetSdkVersion crashes in earlier versions
Crash, but set location takes effect
Abnormal log
29 source apis

API 29 calls the setText() method to ensure that the mNextView, which is assigned by the setView call, is not empty

API 29 setView source code

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