Actually in Android, while using intent to move to a different activity using OnClick Listener object I use the snippet
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
TextView numbers = findViewById(R.id.numbers);
numbers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(**MainActivity.this**, NumbersActivity.class));
}
});
}
}
But even the snippet below works well
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
TextView numbers = findViewById(R.id.numbers);
numbers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(**new MainActivity()**, NumbersActivity.class));
}
});
}
}
To highlight the difference I would like to mark the difference in .... Does this mean that while trying to access the Outer Class of the Anonymous Inner class we will have to access it using the snippet "OuterClass.this" inside the Inner class . But when I use "new OuterClass()" instead of "OuterClass.this" inside the anonymous inner class even this gives a correct result. I want to ask whether both the formats are same or they have any hidden difference as a consequence.