• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Fragment android beginner question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
     
    Yl Ong
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I thought an interface cant initiate an instance?
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Yl Ong
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In my example WorkoutListListener is a interface then isnt listener an instance of WorkoutListListener?
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Yl Ong
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Im reading head first android.then do you mean that listener might be a class as it is a instance of WorkoutListListener.
     
    Yl Ong
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    WorkoutListListener listener;
    Isnt this initialising an instance of the interface?
     
    Pete Letkeman
    Bartender
    Posts: 1868
    81
    Android IntelliJ IDE MySQL Database Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
    The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
    https://www.kickstarter.com/projects/paulwheaton/low-tech
    reply
      Bookmark Topic Watch Topic
    • New Topic