Search code examples
javaandroidandroid-activityabstract-classandroid-source

Some Java helps with the Android source please?


For here the Android source of Activity.java.

In particular, the setContentView() method at line 1646:

public void setContentView(int layoutResID) {
    getWindow().setContentView(layoutResID);
}

i would like to find out what it is exactly calling/doing.

Here is my trace ...

At line 642, we know it is of type android.view.Window

private Window mWindow;

But Window.java is an abstract class. And at line 738, the method is abstract also:

public abstract void setContentView(int layoutResID);

Where is the actual function body be implemented?

Back to the Activity.java file, at line 3746, we find some initializations of mWindow:

mWindow = PolicyManager.makeNewWindow(this);
mWindow.setCallback(this);
...

Well, about com.android.internal.policy.PolicyManager's makeNewWindow():

public static Window makeNewWindow(Context context) {
    return sPolicy.makeNewWindow(context);
}

and

private static final IPolicy sPolicy;
Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);
sPolicy = (IPolicy)policyClass.newInstance();

IPolicy.java is an interface, which means no implementation at all.

My trace cannot go any further. Would you please help me out?

In particular, i know Activity.setContentView() will eventually call android.support.v4.app.Fragment.java's Fragment.onInflate() - line 920, Fragment.onAttach() - line 928, Fragment.onCreate() - line 953, Fragment.onCreateView() - line 967, and Fragment.onViewCreated() - line 991.


Solution

  • Where is the actual function body be implemented?

    private Window mWindow;
    

    The mWindow is initialized by :

    mWindow = PolicyManager.makeNewWindow(this);
    

    in the attach() method. Look at the PolicyManager.makeNewWindow method in PolicyManager.java.

    Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);
    sPolicy = (IPolicy)policyClass.newInstance();
    
    public static Window makeNewWindow(Context context) {
        return sPolicy.makeNewWindow(context);
    }
    

    And the POLICY_IMPL_CLASS_NAME is com.android.internal.policy.impl.Policy. So look at the Policy.java. Here it is:

    public Window makeNewWindow(Context context) {
        return new PhoneWindow(context);
    }
    

    And the setContentView is implmenented in the PhoneWindow.java