0

What is the difference between this and ActivityClass.this when you pass in to intent constructor

for example given 2 activities: ActivityOne and ActivityTwo

the following doesn't work (compile error)

        @Override
        public void onClick(View v) {
            Intent intent = null;
            Log.i(TAG, this.toString());
            Log.i(TAG, ActivityOne.this.toString());
            intent = new Intent(this, ActivityTwo.class);
            startActivity(intent);
        }

while this works

        @Override
        public void onClick(View v) {
            Intent intent = null;
            Log.i(TAG, this.toString());
            Log.i(TAG, ActivityOne.this.toString());
            intent = new Intent(ActivityOnethis, ActivityTwo.class);
            startActivity(intent);
        }

The logging info from the log is

activitylab.ActivityOne$1@41fc5818
activitylab.ActivityOne@41fa2948

although they are different, but the common answer for passing activity is usually using this, I am not why I am running into this problem

Kevin
  • 2,187
  • 4
  • 26
  • 35
  • 2
    This is basic java.In intent first param is Context.You are using it in your OnClick and passing this,means it will point to your onclick not your class as it is in OnCLick block.Adding class name before this ensures it takes the context of your activity and not your method. – Anirudh Sharma Aug 03 '15 at 06:36
  • I guess this will give you the solution. Try this. http://stackoverflow.com/questions/18941007/difference-between-myactivity-this-myactivity-class-this – Amsheer Aug 03 '15 at 06:41
  • You have to use YourActivity.this reference in overridden methods only this which taken inner reference instead class. – Haresh Chhelana Aug 03 '15 at 07:03

3 Answers3

7

I'm assuming that your onClick method is inside an anonymous class instance (though you didn't include the full code, the ActivityOne$1 output you see when printing this is how anonymous inner classes are displayed), so this refers to the anonymous inner class instance, and not the ActivityOne instance containing this instance.

ActivityOne.this returns a reference to the containing ActivityOne instance, which is what you are supposed to pass to the Intent constructor.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

This is basic Java and is answered here.

To recap, you can use Class.this to reference an instance of a class when it is not visible with a bare this, for example from inner classes. In your onClick example this will refer to an anonymous instance of the Listener you are using, while ActivityOne.this will refer to the ActivityOne instance containing the listener.

Community
  • 1
  • 1
milez
  • 2,201
  • 12
  • 31
0

ActivityOne.this == Refrence to context

ActivityTwo.class == Reference to class, this is its class name

this == It is Current Type, If you are in Thread then it is Thread Type; if you are in Activity then it is Activity Type; if you are in your custom class then it is type of that particular custom class

When you do the this then you get an error because you must not be in main thread in this you can use getApplicationContext()

When you use myActivity.this It Knows that it will be started from this activitie's context.

Amol Patil
  • 491
  • 4
  • 9