-2

Trying to call a function from an activity, but I need the specific instance of that activity to do so.

Is there a way to call that specific activity from the application?

If not, is there a way to start an activity from the application so that I always have access to the instance I start running? I tried this, and edited the manifest, but the app never started...

Skorpius
  • 2,135
  • 2
  • 26
  • 31

2 Answers2

2

As concerned with the limited details in this question, I think your requirement is to call a function in an activity that needs that activity itself as the parameter. I think you can do it like this.

Activity actiity=this;  
yourMethode(activity)
{
//body of your methode
}   

Whenever you use the variable "activity", you can get an instance to the current activity.

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55
0

I think you are talking about casting the context to get the type of activity it represents. You can do it like this. But be careful if the context is not of that type you will most likely cause a crash.

((MainActivity) mContext).myMethod();

This is not really recommended as it will cause some tight coupling between the class and the activity.

Eoin
  • 4,050
  • 2
  • 33
  • 43