4

Is there any difference between Activityname.this() & this in Android?

I am trying to open an activity from same activity with button in dialog box? I am using getApplicationContext() in intent. In some mobiles it works, but in others it force closes?

Between ActivityName.this and this which one I should use & why?

source.rar
  • 8,002
  • 10
  • 50
  • 82
Shruti
  • 285
  • 1
  • 3
  • 13

2 Answers2

11

Is there any difference between Activityname.this() & this in Android ?

This depends on where you are calling it from. If you are inside the Activity, not inside of a listener or inner class like in onCreate then no. They both refer to the Activity context.

If you are say inside of an onClickListener then yes. this refers to the listener and you need to use ActivityName.this or something like

someButton.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        Intent i = (v.getContext(), NextActivity.class);   use the button context which will be the same as the activity context
        startActivity(i);
     }
});

This will be the same as when using a Dialog or AlertDialog, you will want to use ActivityName.this

This is an answer that talks about the difference of Contexts but there's a better one I will see if I can find

A great Context explanation

Edit for more completeness

AFAIK, getApplicationContext() or ActivityName.this is fine for Toasts. The example in the docs uses getApplicationContext(). But the Toast Docs says

Parameters context The context to use. Usually your Application or Activity object.

So there may be certain instances where one is better but I have always used Activity Context and I guess I will until I am corrected on this.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • can i use MainActivity.this() instead of getApplicationContext() every where in same activity ,if i am in same MainActivity ? – Shruti Sep 09 '13 at 18:22
  • 1
    Yes, and for most things it will be better than using `getApplicationContext()`. I can't find the answer but CommonsWare has a good explanation on the differences. `Context` can be confusing when you first start with Android – codeMagic Sep 09 '13 at 18:24
  • I found it and added it to my answer – codeMagic Sep 09 '13 at 18:26
  • I agree...that guy normally does a decent job. Like he knows a thing or two or something ;) – codeMagic Sep 09 '13 at 18:34
  • one more question. will replacing getApplicationContext() with MainActivity.this() make any effect to Toasts ? Becuase google recommends to use getApplicationContext() in Toast ... – Shruti Sep 09 '13 at 18:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37056/discussion-between-shruti-and-codemagic) – Shruti Sep 09 '13 at 18:44
1

no MyActivity.this is the same thing as just using this when you are in the activity itself and not something like a runnable where this would refer to the runnable and not the context

you should always use this or getActivity() if in a fragment and never use getApplicationContext()

check here for why you shouldn't use getApplicationContext()

getApplication() vs. getApplicationContext()

Community
  • 1
  • 1
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • can i use MainActivity.this() instead of getApplicationContext() every where in same activity ,if i am in same MainActivity ? – Shruti Sep 09 '13 at 18:23
  • yes you certainly can and that would be the preferred way to get context – tyczj Sep 09 '13 at 18:24