Skip to content Skip to sidebar Skip to footer

Intent.setclass(getactivity, .class) Vs Intent(this, .class)

I had this problem Launching a new activity - android and don't understand why the original code was wrong. Also, even more confusing later on in the activity i have the following

Solution 1:

In this example you are extending DialogFragment, this does not extend from Activity but Fragment.

Here you are calling the getActivity() method of Fragment which returns the attached Activity object for the Fragment.

In the previous question the class was extending Activity, so 'this' is a reference to an Activity.

The complication arises because in the previous question you were accessing 'this' from an anonymous inner class, which dosen't extend from Activity so to access the enclosing object you have to specify the name of the enclosing class i.e MainActivity.this.

Here's the Oracle tutorial on inner classes, the syntax can be confusing at first.

http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

Post a Comment for "Intent.setclass(getactivity, .class) Vs Intent(this, .class)"