Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android context outside of Activity class

I am trying to handle exceptions in my application. I am trying to log the exception, then use Toast to alert the user that there was a problem. I have this working find in all my class's that extend Activity.

However, in any class that does not extended activity I can not use the toast method as I can't get the current context. Is there a simple way to get around this or should all my class's extend Activity?

like image 870
Joseph Avatar asked Mar 15 '12 17:03

Joseph


People also ask

What is difference between activity context and Applicationcontext?

They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.


1 Answers

You just pass Context When You call Non-Activity class from Activity class call Like

YourNonActivtyClass obj = new YourNonActivtyClass(Activity.this);
like image 184
Samir Mangroliya Avatar answered Sep 21 '22 08:09

Samir Mangroliya