Search code examples
androidandroid-fragmentsandroid-context

getActivity() vs this.getActivity() in Fragment


I am new in Android, and I'd like to know if there is a difference between getActivity() and this.getActivity() in Fragment clases. For exemple we have a method in a siple class(doesn't extend Activity or Fragment) like:

 public static void method(Context context){

... some code

    }

If we want to use it, just call it in our fragment class:

MyMethodClass.method(getActivity());

or

MyMethodClass.method(this.getActivity());

I know both are working but I need a proffesional opinion.

Thanks.


Solution

  • They are the same. The this keyword refers to the current object.

    public class Car {
        int speed = 10;
    
        public void move() {
            //using this.speed or speed makes no difference here
        }
    }