3

I am trying to get the context of the app to print a Toast message. The function that I use in Java is the following:

Toast.makeText(getApplicationContext(), "Hey! I'm working", Toast.LENGTH_LONG).show();

But now, that I need to implement it in Kotlin, (language that I'm starting to learn) feel a bit lost. I have tried the following code (given in this post) but it doesn't work as I am not able to get the context of the app.

Toast.makeText(this@CoreMainActivity, "Its toast!", Toast.LENGTH_LONG).show()

Also, I am not able to get the context with getApplicationContext() as described in the documentation.

I would be thankful to know how to call the getApplicationContext(), and to understand what exactly this@CoreMainActivity does (in the post this@CoreMainActivity). Note that coreMainActivity is a Java file.

Thanks!

sandpat
  • 1,478
  • 12
  • 30
Juan CA
  • 156
  • 4
  • 15

4 Answers4

5

According your code

If this works

Toast.makeText(getApplicationContext(), "Hey! I'm working", Toast.LENGTH_LONG).show();

Then it should also work

Toast.makeText(applicationContext, "Its toast!", Toast.LENGTH_LONG).show()

Update: According your implementation use activity instead of applicationContext that you get as parameter

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • That's Java, I want Kotlin!! – Juan CA Jan 01 '20 at 18:10
  • @JuanCA, `Toast.makeText(applicationContext, "Its toast!", Toast.LENGTH_LONG).show()`. It is in *Kotlin* – Md. Asaduzzaman Jan 01 '20 at 18:11
  • Can you add more details where you using this code and get `Unresolved reference`? – Md. Asaduzzaman Jan 01 '20 at 18:13
  • This would be inside the method `onOptionsItemSelected`. More information about the method [here](https://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected(android.view.MenuItem)). I know it's a very wide error, but I'm not very sure what else can I specify. Thanks for the help – Juan CA Jan 01 '20 at 18:24
  • It should not happen. Probably you missed something. Can you post your own implementation here so that I can investigate more? – Md. Asaduzzaman Jan 01 '20 at 18:27
  • Sure! Here goes: ` fun onOptionsItemSelected(item: MenuItem) = Toast.makeText(applicationContext, "Its toast!", Toast.LENGTH_LONG).show() when (item.itemId) { android.R.id.home -> { menuClickListener.onHomeMenuClicked() true } else -> false } ` – Juan CA Jan 01 '20 at 18:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205193/discussion-between-md-asaduzzaman-and-juan-ca). – Md. Asaduzzaman Jan 01 '20 at 18:34
2

One of the following should work:

getContext()
getActivity()
this
CoreMainActivity.this
Tahir Ferli
  • 636
  • 4
  • 16
1

This should work for you:

fun Context.toast(message: CharSequence) = 
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

You can place this anywhere in your project, where exactly is up to you. For example, you can define a file mypackage.util.ContextExtensions.kt and put it there as a top level function

Whenever you have access to a Context instance, you can import this function and use it:

fun myFun(context: Context) {
    context.toast("Hello world!")
}

from this link : https://stackoverflow.com/a/36826336/6430917

Watercayman
  • 7,970
  • 10
  • 31
  • 49
Dv3LaH
  • 11
  • 2
1

Here are the various things we should know.

1.) Application Context - getApplicationContext() will use.

2.) Activity context - coreMainActivity.this will use (As you mentioned your java clas)

Now come on the Kotlin.

1.)Application Context - applicationContext will use

2.)Activity context - this@CoreMainActivity will use

As per the guideline avoid using application context and use activity context as per need.

NOTE: In your java class this@CoreMainActivity will not work.

Pawan Soni
  • 860
  • 8
  • 19