Take the 2-minute tour ×

I often find myself needing to access methods that require referencing some activity. For example, to use getWindowManager, I need to access some Activity. But often my code for using these methods is in some other class that has no reference to an activity. Up until now, I've either stored a reference to the main activity or passed the context of some activity to the class. Is there some better way to do this?

share|improve this question
+1 This is a great question! This is a problem that lots of developers have: How do I figure out what the currently running (or "top") Activity is? –  David Wasser Jun 27 '12 at 14:24
My personal opinion is that it sucks that Google hasn't made a simple API method to access the currently active activity regardless where you are in code. After all, Android knows what it is and keeps track of it. Why not be nice to the rest of us poor boys and let us in on it :-( –  AndroidDev Jun 27 '12 at 14:29
if you need some methods through out of all of your activities which do simply the same, wouldn't it be better to create an abstract class which implements all these methods and let your activities derive from it? or do I get you wrong? –  herom Jun 27 '12 at 14:43
herom, you shouldn't have to bother creating anything. If Android knows what the current activity is, it should make a reference to it easily available. –  AndroidDev Jun 27 '12 at 14:51
sorry for the (maybe stupid) question, but I can't think of a situation where I would need this. could you give me a hint, or describe a situation of yours where you need this (just for clearing things, so I can understand what you want to do)? thanks in advance! –  herom Jun 27 '12 at 14:56
add comment

7 Answers

You can implement the necessary methods in your activity and implement a Handler. Then, simply pass a handler instance to your classes, where you can obtain a message for handler and send it to target.

share|improve this answer
If I'm going to do that, I might just as well pass the context of the activity to the class. –  AndroidDev Jun 27 '12 at 13:52
add comment

Passing context is better way for refrence Activity.

You can pass Context to another class.

IN Activity ::

AnotherClass Obj  = new AnotherClass(this);

IN Another Class

class AnotherClass{

public AnotherClass(Context Context){


share|improve this answer
But if there are several activities loaded only one of them will be active at any moment, so I suspect that there is a method to access whatever the current activity is without having to pass around a context. –  AndroidDev Jun 27 '12 at 13:45
Please explain why is this a better way? –  Slawa Oct 28 '12 at 22:24
add comment

You can make you application instance a singleton, and use it when you need a Context

An example is in this question:
Android Application as Singleton

This way, when you need a Context, you can get it with
Context context = MyApplication.getInstance()

This might not be the cleanest solution, but it has worked well for me so far

share|improve this answer
Would never work if I was writing the classes and gave them to someone else who could not use a Singleton. –  AndroidDev Jun 27 '12 at 13:57
That's true... :( –  Bojan Radivojevic Bomber Jun 27 '12 at 13:57
add comment

We built a framework for this. We have a BaseActivity class that inherits from Activity and it overrides all the lifecycle methods and has some static (class) variables that keep track of the activity stack. If anything wants to know what the current activity is, it just calls a static method in BaseActivity that returns the activity on top of our privately-managed stack.

It is kinda hacky, but it works. I'm not sure I would recommend it though.

share|improve this answer
add comment

There are many ways for Activities communication.

you can use:

  • the startActivityForResult method

  • a system of broadcast message and receiver (you can broadcast an event from the actual activity, and register a receiver in the target activity. Remember that the target activity must be previously initialized and non finished)

  • as you say, store a reference of the target activity wherever you need.
share|improve this answer
I don't want communication. Just a reference. –  AndroidDev Jun 27 '12 at 13:47
add comment

Handle the Intent in the class you want to do these methods, and send your information to it in a Bundle like so:

    Intent i = new Intent("android.intent.action.MAIN");
    i.setComponent(new ComponentName("com.my.pkg","com.my.pkg.myActivity"));
    Bundle data = new Bundle();



Then use an OnActivityResultListener to grab the new data.

share|improve this answer
No, that is not what I am looking for. All I want is a reference to whatever the current activity is, or the main one. You can have a class call a class and go quite deep. There should not be a need to pass a context to each class. –  AndroidDev Jun 27 '12 at 13:52
Don't understand the downvote, this would technically work. –  John Satriano Jun 27 '12 at 14:28
add comment

Just a guess since I haven't done this but it might work.

1) Get your applicationContext by making your Android Application class a Singleton.

2) Get your ActivityManager class from the context.

3) Get a list of RunningTaskInfos using getRunningTasks() on the ActivityManager.

4) Get the first RunningTaskInfo element from the list which should be the most recent task launched.

5) Call topActivity on that RunningTaskInfo which should return you the top activity on the activity stack for that task.

Now, this seems like a LOT more work than any of the other methods mentioned here, but you can probably encapsulate this in a static class and just call it whenever. It seems like it might be the only way to get the top activity on the stack without adding references to the activities.

share|improve this answer
Why the downvote? An explanation would be nice... –  onit Jun 27 '12 at 14:20
I didnät give you the downvote, but...one of the problems with this technique is that you need permissions to getRunningTasks() and that scares a lot of users. –  David Wasser Jun 27 '12 at 14:23
@DavidWasser That is true, I don't recommend this approach, but I posted it more to show what kind of work you would need to go through to get the currently running activity off the stack without a reference. –  onit Jun 27 '12 at 14:24
add comment

Your Answer


