Toast.makeText is not showing in Android 11 (with SDK 30 emulator)
Asked Answered
S

1

11

According to https://developer.android.com/about/versions/11/behavior-changes-11#toasts, Toasts should still work as normal for Android11 (only setView() was deprecated).

Note that text toasts are still allowed; these are toasts created using Toast.makeText() that don't call setView().

However, I cannot get Toast to show for emulators running SDK30 with

    compileSdkVersion 30
    buildToolsVersion "30.0.3" 
    and with targetSdkVersion 30 
    

The code used was just a simple

    Toast.makeText(getApplicationContext(), "HELLO WORLD", Toast.LENGTH_SHORT).show();

The toast is displayed when I change the targetSdk to 29 (or with devices <SDK30).

Do I need to add any <queries> tags in manifest file for Toasts ? https://developer.android.com/about/versions/11/privacy/package-visibility

I've also tried the following without luck.

  1. rebooting the emulator Toast not showing in Android Q
  2. notification ON Toast message not shown
Supporting answered 13/1, 2021 at 15:4 Comment(5)
Confirmed. Configuration to reproduce: compileSdkVersion 30 and targetSdkVersion 30. Moreover, not just applicationContext, but any other context (e.g. Activity) won't work either. I recommend to create an issue at the Google Issue Tracker.Moskva
Hi! Have you created the issue at the Google Issue Tracker yet?Moskva
I had exactly the same issue and here is my solution stackoverflow.com/a/66460317/7237884Ultun
@Onik: I tried building with compileSdkVersion 30 too, but still, the app crashes on my Samsung S10+ whenever a Toast is called (though it runs fine in the Android Studio device emulator)Jenellejenesia
maybe related: #45128903Jenellejenesia
A
6

First method

Updating of Android Emulator to 30.4.5 version fixed bug for me. P.S. I also updated Android SDK Platform-Tools to 31.0.0 version (may be this helped too).

Menu Tools -> SDK Manager -> SDK Tools, set checkbox of Android Emulator to V-mode, press button Apply, press button OK, confirm.

P.S. Also after updating of those the Android Studio and emulators were closed and project was cleaned before new compilation.

Second method

If you get this bug while using the last version of Android Emulator there is another solution. You can try the cold boot of emulator. Menu Tools -> AVD Manager. Find your emulator name, click button ▼ on the right side of it, select Cold boot now button.

Arlie answered 2/3, 2021 at 13:12 Comment(1)
check my solution: https://mcmap.net/q/1158588/-toast-doesn-39-t-work-on-real-device-after-adding-permissionsJenellejenesia

© 2022 - 2024 — McMap. All rights reserved.