How to force derived class to call super method? (Like Android does)

前端 未结 8 2261
余生分开走
余生分开走 2020-12-08 04:00

I was wondering, when creating new Activity classes and then overriding the onCreate() method, in eclipse I always get auto added: super.onCr

相关标签:
8条回答
  • 2020-12-08 04:28

    Here's the source of Activity#onCreate() - it is almost all comments (original - see line ~800):

    /**
     * Called when the activity is starting.  This is where most initialization
     * should go: calling {@link #setContentView(int)} to inflate the
     * activity's UI, using {@link #findViewById} to programmatically interact
     * with widgets in the UI, calling
     * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve
     * cursors for data being displayed, etc.
     *
     * <p>You can call {@link #finish} from within this function, in
     * which case onDestroy() will be immediately called without any of the rest
     * of the activity lifecycle ({@link #onStart}, {@link #onResume},
     * {@link #onPause}, etc) executing.
     *
     * <p><em>Derived classes must call through to the super class's
     * implementation of this method.  If they do not, an exception will be
     * thrown.</em></p>
     *
     * @param savedInstanceState If the activity is being re-initialized after
     *     previously being shut down then this Bundle contains the data it most
     *     recently supplied in {@link #onSaveInstanceState}.  <b><i>Note: Otherwise it is null.</i></b>
     *
     * @see #onStart
     * @see #onSaveInstanceState
     * @see #onRestoreInstanceState
     * @see #onPostCreate
     */
    protected void onCreate(Bundle savedInstanceState) {
        mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
                com.android.internal.R.styleable.Window_windowNoDisplay, false);
        mCalled = true;
    }
    

    so, my guess would be that the ADT Eclipse plugin is what's auto-adding that call to super.onCreate() for you. It's a total guess, though.

    0 讨论(0)
  • 2020-12-08 04:30

    To answer your actual question, the auto-creation of the call to super.onCreate() is a feature of the ADT plugin. In java, you cannot directly force a subclass to call the super implementation of a method, afaik (see the pattern described in other answers for work-around). However, keep in mind that in Android, you are not instantiating Activity objects (or Service objects) directly - you pass an Intent to the system and the system instantiates the object and calls onCreate() upon it (along with other lifecycle methods). So the system has a direct object reference to the Activity instance and is able to check (presumably) some Boolean that is set to true in the superclass implementation of onCreate(). Although I don't know exactly how it is implemented, it probably looks something like this:

    class Activity
    {
      onCreate()
      {
        superCalled = true;
        ...
      }
      ...
    }
    

    And in the "system" level class that receives the Intent and instantiates the Activity object from it:

    ...
    SomeActivitySubclass someActivitySubclassObject = new SomeActivitySubclass();
    someActivitySubclassObject.onCreate();
    if (!someActivityObject.isSuperCalled())
    {
      Exception e = new Exception(...) //create an exception with appropriate details
      throw e;
    }
    

    My guess is it's probably slightly more complex than that, but you get the idea. Eclipse automatically creates the call because the ADT plugin tells it to, as a convenience. Happy coding!

    0 讨论(0)
提交回复
热议问题