-2

Providing finish() and this.finish() in onPause() or onStop() method is same?

molnarm
  • 9,856
  • 2
  • 42
  • 60
John Victor
  • 615
  • 4
  • 9
  • 22

5 Answers5

3

Yes. Please become familiar with meaning of this. -> it's value is the reference to the current object. For example, if you have a class named Foo, and it has method named method(), then this in it would be a reference to a instance of the Foo (that is: a Foo object). Usually you do not need to use this.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
2

this in any context refers to the containing class. So, if you are using the method inside an Activity, then this.finish() is same as finish(). However, if you are using this in a different class type, you may not have this.finish()

Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
2

Even though the question is 3 years old.I prefer to torch some light over the present and future researchers.

this is just an object reference.You don't have to use this every time ,other than you need to get a reference of parent class from a child class instance.

Let's consider an example when using Thread class.

public class A
{
   public A()
    {
        new Thread(new Runnable()
         {
             public void start()
             {
                  B child=new B(A.this);//In this scenario,'A.this' refers to the parent class 'A' in which the 'Thread' class instantiated.If you simply pass 'this' ,then it would refer to the 'Thread' class as this statement executed in the current scope.
             }
         }).start();
    }
}
public class B
{
A parent;
    public B(A parent)
    {
        this.parent=parent;//'this' refers to the class B ,so that it can access the global variable 'parent' ,then assigns it with the local variable 'parent' passed through the constructor.
    }
}

Like listed above ,there are different usages of this keyword.Its better to refer the oracle's documentation over here https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

ShihabSoft
  • 835
  • 6
  • 15
0

finish() and this.finish() is the same.

For the other part of the question, please read about the Activity lifecycle.

MarchingHome
  • 1,184
  • 9
  • 15
0

In your case It's the same. It's sometimes important to use this->... if you have an member and an method parameter with the same name like in the following example:

    class foo{

    int number;

    void setNumber(int number);

    }

so you can write in your method

    void foo::setNumber(int number)
    {
    this->number = number; 
    }

And so It's clear which element you have used. But be careful don't use the same names it's not really nice.

retinotop
  • 483
  • 11
  • 17