2

I have an application that uses Toast and I have tested it on a Samsung Galaxy II using Android 4.3 and it works fine.

I have recently gotten a Nexus 5 running Android 4.4 KitKat and when I try to load the Toast it doesn't appear.

Has Toast been removed or the syntax altered in KitKat?

This is the code I'm using to make and call the Toast:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
    int itemId = item.getItemId();
    switch (itemId) {

    case R.id.action_settings:
        Log.e("TOAST","This was called");
        Toast.makeText(MainActivity.this, "This is my Toast message!",Toast.LENGTH_LONG).show();
        break;
    }
    return true;
}

I have Debugged the code and stepped through this part line by line so it is definitely being called, but nothing happens.

PriestVallon
  • 1,519
  • 1
  • 22
  • 44
  • Does the `getBaseContext()` work as the first argument ? – Chintan Soni Dec 04 '13 at 11:17
  • Regarding `Context`, perhaps [this answer on other question](http://stackoverflow.com/a/10347346/2821954) may help you. – Andrew T. Dec 04 '13 at 11:19
  • Check this link: http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context – Chintan Soni Dec 04 '13 at 11:21
  • Have you tried `getApplication()`? – AlexS Dec 04 '13 at 11:29
  • getBaseContext() and getApplication() don't work. The toast is still hidden. – PriestVallon Dec 04 '13 at 11:46
  • 1
    Code looks good (modulo the typo in `MainActivty`) and works on my kitkat. The debugger can lie to you - add e.g. logging to the switch-case to confirm the case is taken. – laalto Dec 04 '13 at 12:12
  • I added logging and it was logged every time it should have. I tried it on a 4.4 emulator and it worked fine. I'm starting to wonder is it do with the Nexus 5 device rather than KitKat – PriestVallon Dec 04 '13 at 12:22
  • 1
    Dude, take a look at this: http://stackoverflow.com/questions/25507416/toasts-stopped-working-in-4-4-2 maybe is the same to you too. – user3618359 Jan 16 '15 at 11:02

2 Answers2

2

Use this instead of getApplicationContext()

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
  • 3
    any specific reason why should he use Activity.this instead of getApplicationContext()? – Paresh Mayani Dec 04 '13 at 11:17
  • @PareshMayani: Here just a `context`(reference) of `current activity` is required rather than context of `application` – Mehul Joisar Dec 04 '13 at 11:23
  • @MehulJoisar Both ApplicationContext as well as ActivityContext can be used with Toast, since a Toast is not tied to a window and can be shown from everywhere in your app. See [Toast.makeText](http://developer.android.com/reference/android/widget/Toast.html#makeText%28android.content.Context,%20java.lang.CharSequence,%20int%29) – AlexS Dec 04 '13 at 11:27
  • Using this or YourActivity.this makes no difference. It still doesn't appear but it works fine on an the device running Android 4.3 – PriestVallon Dec 04 '13 at 11:43
  • @PriestVallon: It is strange !! Can you post the code from where u r firing `toast` ? – Mehul Joisar Dec 04 '13 at 11:47
  • @PriestVallon: Look at Anil's answer. I've updated it by mistake. – Mehul Joisar Dec 04 '13 at 12:31
0

Better use YourActivity.this instead of getApplicationContext()

EDIT :

Code seems valid.the issue is either nexus or kitkat.

  • give a try by rebooting your cell.
  • give a try by firing the toast from onResume(). just to make sure that it is working fine from UI thread.
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
Looking Forward
  • 3,579
  • 8
  • 45
  • 65