Using getApplicationContext() in another simple class, NOT in MainActivity.java

7.2k Views Asked by At

I've created a simple class for my Android application called coreTuts. I'm done with tying it in to my MainActivity.java, activity_main.xml, and so on, like this:

MainActivity.java

coreTuts tuts = new coreTuts();
 
public void displayToast(View view)
{
 tuts.sampleToast();
}
This is what my coreTuts.java looks like:

coreTuts.java

public class coreTuts{

 
 //Do a toast notification
 public void sampleToast()
 {
  
  Toast toast = Toast.makeText(getActivity().getApplicationContext(),
          "This is a message displayed in a Toast",
          Toast.LENGTH_SHORT);
      
  toast.show();
 }

}

I couldn't decide whether I should usegetActivity().getApplicationContext()or just getApplicationContext() on my Toast because either code doesn't work.

In fact, these are my questions:

  1. I understand contexts in Android are kinda like habitats to animals. Am I right if I look at getActivity() and getApplicationContext() that way?
  2. How do I make getApplicationContext() work in another class so that I can run my toast, or is that even allowed?

Thank you!

3

There are 3 best solutions below

2
On BEST ANSWER

your coreTuts should be like below

public class coreTuts{


//Do a toast notification
public void sampleToast(Context context)
{

    Toast toast = Toast.makeText(context,
            "This is a message displayed in a Toast",
            Toast.LENGTH_SHORT);

    toast.show();
}

}

and you can invoke it like below,

coreTuts tuts = new coreTuts();

public void displayToast(View view)
{
   tuts.sampleToast(view.getContext());
}

Note: view cannot be null

4
On

As your class CoreTuts is not inheriting from Activity nor any other Context subclass (Activity is a child of Context) you can't access to your context the way you are trying to. You need to explicitly pass it to your sampleToast method, like this:

public class coreTuts{

    //Do a toast notification
    public void sampleToast(Context context) {
        Toast toast = Toast.makeText(context,
                "This is a message displayed in a Toast",
                Toast.LENGTH_SHORT);
        toast.show();
    }

}

And in your activity:

coreTuts tuts = new coreTuts();

public void displayToast(View view) {
    // Pass your activity as the context
    tuts.sampleToast(this);
}
0
On

Pass a context to your coretuts class when you're creating it's object. Your coretuts class would look like this.

public class coreTuts{

private Context mContext;

public coreTuts(Context context) {
    mContext = context;
}

//Do a toast notification
public void sampleToast()
{

    Toast toast = Toast.makeText(mContext,
            "This is a message displayed in a Toast",
            Toast.LENGTH_SHORT);

    toast.show();
}

}

Now, when you're creating an object of this class inside your MainActivity, simply pass the context

// Pass your context. You can also use getApplicationContext() instead of MainActivity.this
coreTuts tuts = new coreTuts(MainActivity.this);

// You don't really need a view argument for this method. 
// It could just be public void displayToast() {...}
public void displayToast(View view)
{
    tuts.sampleToast();
}