1. Download our Official Android App: Forums for Android!

Apps Overriding

Discussion in 'Android Development' started by vag, Jul 30, 2011.

  1. vag

    vag Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    12
    Joined:
    Jul 7, 2011

    Jul 7, 2011
    12
    0
    15
    Hi all.
    When we override method in subclass, we call in this method the superclass method, for example
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    width = w ;
    height = h ;
    Log.d(TAG, "onSizeChanged: width " + width + ", height "+ height);
    super.onSizeChanged(w, h, oldw, oldh);
    }
    so why we need to call super.onSizeChanged().
    I delete the line super.onSizeChanged(), and the result is the same as with it.


    or the same in onCreate method, we call super.onCreate.

    sorry for bad english, and thaks for replays))
     

    Advertisement

  2. QuackWare

    QuackWare Newbie
    Rank:
    None
    Points:
    16
    Posts:
    22
    Joined:
    Jul 14, 2010

    alostpacket likes this.
  3. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    I *think* it goes something like this (please correct me if wrong)

    In Android, you MUST call through to the super class for component lifecycle methods that you override.

    Such as (as QuackWave said) onCreate(), onDestroy(); onResume(); onStart(); onStop(); etc

    The reason you must do this is because Android has to do work behind the scenes (such as loading your app into memory). So it must do its work before you can do yours.

    Almost everything else would be optional to call through to the super method.

    In some cases the super method will do nothing. In others, the super method will help process something.

    a good example would be onTouchEvent()

    You could process the event if it's a specific type of event and NOT call super.

    Or, if it's not the event you want, you could hand it off to the super class to process normally.
     
  4. vag

    vag Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    12
    Joined:
    Jul 7, 2011

    Jul 7, 2011
    12
    0
    15
    Thanks for answers))
     
  5. redcrab

    redcrab Newbie
    Rank:
    None
    Points:
    36
    Posts:
    14
    Joined:
    Aug 9, 2011

    Aug 9, 2011
    14
    4
    36
    my two cents....

    "super" must be (not compile otherwise) at first line in method only for constructor ... other overridden method can place "super" anywhere in the method ... for sure for the framework(android here) that may mean something important .. but may be not forbidden if you know what you do ;)

    and sometime in not so rare case you may want to not invoke super ... to be able to replace the complete method behaviour ... I think this is not the case in current android framework .. but on other home made classes it may appends.


    hope to give more light ...

    hope that help

    have fun
    have fun
     

Share This Page

Loading...