1

How do I get the instance of the currently visible activity in Android? I've read that I can get the ComponentName of the activity by using ActivityManager to get a list of tasks and messing with that, but that's a recipe for disaster.

Is there a way to get an instance of the topmost activity, or if the activity isn't mine and I can't access it, just null?

Ron
  • 1,989
  • 2
  • 17
  • 33
  • Please define "mine". An activity from an application you wrote, or an activity from the same application instance the accessor code is running from? – mikołak Aug 07 '13 at 13:37
  • http://stackoverflow.com/questions/11411395/how-to-get-current-foreground-activity-context-in-android ? – Skaard-Solo Aug 07 '13 at 13:38

1 Answers1

-2

TO get instance of currently visible activity, get its context as:

Context mContext = getApplicationContext();

or

mContext = this;

now use it for this activity related tasks.

OR to get instance of another activity; keep a static ActivityClass instance somewhere else and use getter setter to get set this instance like:

public void setActivity(MyActivity activity) {
         myActivity = activity;
    }

    public MyActivity getMyActivity() {
        return myActivity;
    }
Master
  • 2,945
  • 5
  • 34
  • 65