16

I created an custom view and there require Activity reference to perform some Handler related operation. I have idea about getContext() is a way to get Context but is there any way to get Activity reference for same?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
CoDe
  • 11,056
  • 14
  • 90
  • 197

4 Answers4

32

It should be fine to just cast the context to Activity:

MyActivity myActivity = (MyActivity) getContext();
Jan
  • 1,359
  • 1
  • 13
  • 18
  • 1
    same I did ...and it work for me...thanks – CoDe Oct 25 '13 at 10:55
  • 1
    Please observe that getContext() may not return the host activity on some scenarios, like appwidgets or when you inflate a view without add it on the container, for some reason. – Renascienza Dec 28 '14 at 16:59
  • 6
    It may also return a ContextThemeWrapper, which cannot be cast to Activity. –  Dec 30 '14 at 16:49
  • 1
    True, thanks for the hint. Be carefull if you are outside of "ordinary" application context. – Jan Jan 01 '15 at 13:40
  • @user153275 when would this happen? – ono Nov 01 '17 at 04:23
26

Casting getContext() to Activity (e.g. (Activity)getContext();) may not always return an Activity object if your View is not called from an Activity context.

So for that,

    public Activity getActivity() {
        Context context = getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity)context;
            }
            context = ((ContextWrapper)context).getBaseContext();
        }
        return null;
    }

"while" is used to bubble up trough all the base context, till the activity is found, or exit the loop when the root context is found. Cause the root context will have a null baseContext, leading to the end of the loop.

Arunendra
  • 2,034
  • 26
  • 22
  • This is the best answer and exactly what I came here for. Thank you it saved me a lot of time. – domenukk Aug 17 '16 at 13:21
  • Thanks, I just replaced ((Activity) getContext()).runOnUiThread( (new Thread(new Runnable() { with getActivity().runOnUiThread( (new Thread(new Runnable() { and it works... – Christian Mar 08 '17 at 20:49
1

Pass context in the Constructor of View class like this

View Class

public class DrawView extends View {

    Context actContext;

    public DrawView(Context context) {
        super(context);

        actContext=context;
    }
}

and in Your activity class

DrawView drawView=new DrawView(this);
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • Usually I do like that, works fine. Sometimes (in some situations) extra checks will be needed to avoid crash. – Andrew Feb 27 '15 at 22:30
0

You can get your activity reference by sending the context of your activity in the constructor of your handler when defining its objects and the constructor defination in the handler class is defined below:

private Context mContext;
public MyHandler(Context context) {
    mContext=context;
}

and in yor main activity class you can make its object as follows:

MyHandler ca=new MyHandler(MainActivity.this);
Bondax
  • 3,143
  • 28
  • 35
jyomin
  • 1,957
  • 2
  • 11
  • 27