*please ignore the red lines i just copy the code there to show you guys*
this is a code i found in a book.the code is trying to communicate between fragments with the help of the mainActivity.
"this.listener = (WorkoutListListener) activity;"
1)why in the onAttach method we have to cast the interface to the activity?
2)why is it equals to this.listener(instance of
The listener object itself is a WorkoutListListener object or an object which supports the WorkoutListListener interface.
I believe that once you understand casting of objects/classes that most of your questions regarding this will be answered.
Luckily Oracle has free tutorials (Oracle calls them Trails) which may help you understand this.
This Oracle Java Trail talks about casting and more https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html which you may find helpful.
It may be wiser to start with this Oracle Java Trail which is a lead in to Interfaces and Inheritance https://docs.oracle.com/javase/tutorial/java/IandI/index.html.
By the way are you aware that Google/Android has free tutorials on Android development?
Here is the tutorial on fragment communication https://developer.android.com/training/basics/fragments/communicating.html
Yl Ong wrote:I thought an interface cant initiate an instance?
You cannot instantiate an interface. However you can cast an object to an interface so that you can use those interface methods.
When you cast an object to an interface you can only access the interface properties/methods, but you can group objects together if you need to based on this common interface.
If we take the example of automobile, which implement an interface of vehicle which has a method drive. So each of the following all now how to drive: car, truck, bicycle, street bike, train.
Now you want something that can drive you from home to work and you don't care what as long as it can drive.
However a car drives differently then a train and a drives differently then a street bike. You don't need to know that, you only need to know that it can drive.
And when you do this all that you can access the the drive method.
Here is some Java code that works as is which may help explain the point.
Driving.javaIf you save the above into a file named Driving.java, compile and run it you may understand this a bit better.
Yl Ong wrote:In my example WorkoutListListener is a interface then isnt listener an instance of WorkoutListListener?
Yes, remeber that if a class implements an interface then it is said to be an instance of the interface.
You can even do tests to see if this is true using the instanceof operator so that you can do safe casting as shown below.
This code is casting the variable activity to make it a type of WorkoutListListener and then assigning that to the listener object.
Yl Ong wrote:why in the onAttach method we have to cast the interface to the activity?
To answer this question:
onAttach happens fairly close to the beginning of a fragment's life cycle.
A fragment is a pretty big part of your app (usually) and it hosts other controls.
So you as soon as you can to make sure that the interface (listener) is attached to the fragment.
The interface (listener) could in theory change how the fragment is displayed/rendered or fire off some other event.
We know that activity is an instance of the interface WorkoutListListener, but when we assign it to the listener object we need to tell the compiler that it is.
If we do not then it may not compile (worst case) or it will compile with warnings.
Can you tell me what book are you using?
Can you tell me what editor/IDE you are using?
The free Android tutorials have full examples with documentation and comments as well. They also explain each step as they go, so you can go from no app to working app fairly quickly.
You may want to also check out the Android API guide found here https://developer.android.com/guide/.
However, it is not always that easy to consume the information in the API guides, so you may want to augment this with other resources.
Yl Ong wrote:WorkoutListListener listener;
Isnt this initialising an instance of the interface?
Interfaces cannot have state properties and they cannot have constructors.
You are not instantiating an instance of the interface.
You are telling Java to temporary forget about everything else about the class that is not related to the interface when you cast an object to an interface.
Those member properties and methods are still there, they just cannot be accessed the usual way.
Yl Ong wrote:do you mean that listener might be a class as it is a instance of WorkoutListListener
If a class does implement an interface then it can be said that the class is an instance of the interface.
If we had:
We can say that the Plant object is an instance of Growing.
We can say that the Animal object is an instance of Growing.
However we cannot not create a Growing object, we can only create a Plant or Animal object, which implements the Growing interface.
We can treat all Plant objects the same way no matter how they implement the Growing interface. However this would exclude all Animal objects.
We can treat all objects that implement the Growing interface the same way, if they are Plant or Animal objects it doesn't matter to us.
The Oracle Java Trail on Interfaces and Inheritance may be able to help explain this better