Jimmy
Jimmy

Reputation: 10361

How to get reference of activity object?

I want to show a messageBox or notification when connection lost in Static Database class. But I can't use getApplicationContext() as the class is static and I tried to call other class called notification but I have an error. So How could I pass activity object to my new class ?

Upvotes: 0

Views: 4183

Answers (3)

Pentium10
Pentium10

Reputation: 208042

How are you doing it's wrong. You should pass a context each time when you call your static methods.

However you can have a static Context property in that class, and you could put in your current context before calling for a method. But this is bad practice! Don't do it!

Upvotes: 2

Bostjan
Bostjan

Reputation: 1397

You can also pass the activity context in the constructor of your class. Code would help though.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007614

You cannot have a "static DB class" that works with the UI in the fashion you are seeking. For any call that may fail, have an Activity (or possibly other Context) be passed in as a parameter to the method.

Upvotes: 0

Related Questions