[Logo]
Forums Register Login
Fragment android beginner question
Fragment android beginner question

*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
WorkoutListListener)

Screenshot_2017-12-07-20-05-37.png
[Thumbnail for Screenshot_2017-12-07-20-05-37.png]
I have a couple of questions:
  • What book are you referring to?
  • What editor/IDE are you using?
  • This code casts the activity to a WorkoutListListener object and assigns it to the listener object.
    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
    I thought an interface cant initiate an instance?
     

    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.
    Please note that in the above code we only instantiate three objects of different types (plus an array at the end) which all support the same interface.
    It does not instantiate any interfaces.
    In my example WorkoutListListener is a interface then isnt listener an instance of WorkoutListListener?
     

    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?
    Seeing as you are just starting out, you may want to check out the free Android tutorials provided by Android/Google found here https://developer.android.com/training/
    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.
    Im reading head first android.then do you mean that listener might be a class as it is a instance of WorkoutListListener.
    WorkoutListListener listener;
    Isnt this initialising an instance of the interface?
     

    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:
  • a class for plants named Plant which implemented an interface for Growing.
  • a class for animals named Animal which implemented an interface for Growing.

  • 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
    https://docs.oracle.com/javase/tutorial/java/IandI/index.html
    Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


    This thread has been viewed 4674 times.

    All times above are in ranch (not your local) time.
    The current ranch time is
    Jun 20, 2018 13:25:11.