如何/在Android中调用onCreate

时间:2014-10-10 02:05:52

标签: android callback android-source

的文件
void android.app.Activity.onCreate(Bundle savedInstanceState)

http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle) 如果活动开始时调用onCreate()。

那么调用onCreate()的方式和位置以及调用它的方式和位置?我在Android源代码中搜索过,并没有找到任何调用onCreate()的代码,其值为savedInstanceState。

2 个答案:

答案 0 :(得分:3)

通过检查onCreate的堆栈跟踪,我发现了答案,如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        throw new Exception("MY_EXCEPTION");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

堆栈追踪:

10-10 10:10:34.139: W/System.err(10667): java.lang.Exception: MY_EXCEPTION
10-10 10:10:34.139: W/System.err(10667):     at com.example.mybrowser.MainActivity.onCreate(MainActivity.java:27)
10-10 10:10:34.139: W/System.err(10667):     at android.app.Activity.performCreate(Activity.java:5246)
10-10 10:10:34.139: W/System.err(10667):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-10 10:10:34.139: W/System.err(10667):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
10-10 10:10:34.139: W/System.err(10667):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
10-10 10:10:34.139: W/System.err(10667):     at android.app.ActivityThread.access$800(ActivityThread.java:138)
10-10 10:10:34.139: W/System.err(10667):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
10-10 10:10:34.139: W/System.err(10667):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-10 10:10:34.139: W/System.err(10667):     at android.os.Looper.loop(Looper.java:136)
10-10 10:10:34.139: W/System.err(10667):     at android.app.ActivityThread.main(ActivityThread.java:5034)
10-10 10:10:34.139: W/System.err(10667):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 10:10:34.139: W/System.err(10667):     at java.lang.reflect.Method.invoke(Method.java:515)
10-10 10:10:34.139: W/System.err(10667):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:807)
10-10 10:10:34.139: W/System.err(10667):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
10-10 10:10:34.139: W/System.err(10667):     at dalvik.system.NativeStart.main(Native Method)
10-10 10:10:34.179: W/IntentResolver(1097): resolveIntent: multiple matches, only some with CATEGORY_DEFAULT
10-10 10:10:34.189: W/ContextImpl(10682): Calling a method in the system process without a qualified user: android.app.ContextImpl.startService:1511 android.content.ContextWrapper.startService:494 android.content.ContextWrapper.startService:494 com.android.keychain.KeyChainBroadcastReceiver.onReceive:12 android.app.ActivityThread.handleReceiver:2410 

所以,答案:答案: onCreate由android.app.Activity.performCreate调用,performCreate由android.app.Instrumentation.callActivityOnCreate调用,... 例如:

final void performCreate(Bundle icicle) {
    onCreate(icicle);
    mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
            com.android.internal.R.styleable.Window_windowNoDisplay, false);
    mFragments.dispatchActivityCreated();
}

答案 1 :(得分:0)

您列出的Android文档顶部显示"' onCreate'被称为初始化活动" ...与您在问题中所述的不同。在活动开始时不一定要调用它。您必须在实际启动之前初始化活动。我已经在代码底部看到了创建,但我相信你在堆栈调用中正在讨论。尽量明确问题。希望我帮忙。该网站上有很多内容,这个网站和android.stackexchange.com