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

Shruti

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?

codeMagic

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there any difference between $@ and "$@"?

From Dev

Difference (if there is any) between ` and ' in javascript

From Dev

Is there any difference between vbNullString and ""?

From Dev

is there any difference between the queries

From Dev

Is there any difference between QRegularExpression and QRegExp?

From Dev

Is there any difference between these two loops?

From Java

Difference between "*" and "Any" in Kotlin generics

From Java

Is there any difference between a GUID and a UUID?

From Dev

Is there any difference between type and class?

From Dev

Is there any difference between `*x` and `x*`?

From Dev

Is there any technical difference between these methods?

From Dev

Is there any difference between sql linq

From Dev

Difference between Future[Any] and Future[_]

From Dev

Any difference between '+' and '&' for String concatenation?

From Dev

What is the difference between `mixed` and `any`?

From Dev

Is there any difference between Pickling and Serialization?

From Dev

Is there any difference between these two loops?

From Dev

There is any difference between this zone syntax?

From Dev

Is there any difference between both of these codes?

From Dev

Is there any difference between the two codes?

From Dev

Is there any difference between nvprof and pgprof?

From Java

What is the difference between ()=>any and {():any} in Typescript

From Dev

What's the difference between "(:any)" and ":any" in CodeIgniter?

From Dev

typescript: any difference between number[] and [number,number]?

From Dev

Any difference between $injector and Inject() in AngularJS?

From Dev

Is there any difference between a feature spec and a view spec?

From Dev

In ScalaTest is there any difference between `should`, `can`, `must`

From Dev

Is there any difference between lists partition and splitwith in Erlang?

From Dev

Is there any difference between ".andReturn(...).anyTimes()" and ".andStubReturn(...)" in EasyMock?

Related Related

  1. 1

    Is there any difference between $@ and "$@"?

  2. 2

    Difference (if there is any) between ` and ' in javascript

  3. 3

    Is there any difference between vbNullString and ""?

  4. 4

    is there any difference between the queries

  5. 5

    Is there any difference between QRegularExpression and QRegExp?

  6. 6

    Is there any difference between these two loops?

  7. 7

    Difference between "*" and "Any" in Kotlin generics

  8. 8

    Is there any difference between a GUID and a UUID?

  9. 9

    Is there any difference between type and class?

  10. 10

    Is there any difference between `*x` and `x*`?

  11. 11

    Is there any technical difference between these methods?

  12. 12

    Is there any difference between sql linq

  13. 13

    Difference between Future[Any] and Future[_]

  14. 14

    Any difference between '+' and '&' for String concatenation?

  15. 15

    What is the difference between `mixed` and `any`?

  16. 16

    Is there any difference between Pickling and Serialization?

  17. 17

    Is there any difference between these two loops?

  18. 18

    There is any difference between this zone syntax?

  19. 19

    Is there any difference between both of these codes?

  20. 20

    Is there any difference between the two codes?

  21. 21

    Is there any difference between nvprof and pgprof?

  22. 22

    What is the difference between ()=>any and {():any} in Typescript

  23. 23

    What's the difference between "(:any)" and ":any" in CodeIgniter?

  24. 24

    typescript: any difference between number[] and [number,number]?

  25. 25

    Any difference between $injector and Inject() in AngularJS?

  26. 26

    Is there any difference between a feature spec and a view spec?

  27. 27

    In ScalaTest is there any difference between `should`, `can`, `must`

  28. 28

    Is there any difference between lists partition and splitwith in Erlang?

  29. 29

    Is there any difference between ".andReturn(...).anyTimes()" and ".andStubReturn(...)" in EasyMock?

HotTag

Archive