I wrote a little program looking like this:
package com.example.lifecycle;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class LifeLogger extends Activity {
private String TAG = this.getClass().getName().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_life_logger);
Log.d(TAG,"onCreate event");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume event");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause event");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop event");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG,"onRestart event");
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
Log.d(TAG,"onCreateView event");
return super.onCreateView(name, context, attrs);
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart event");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy event");
}
}
Which is the main activity.
The LogCat is:
06-11 07:07:10.033: D/com.example.lifecycle.LifeLogger(600):
onCreateView event 06-11 07:07:10.033:
D/com.example.lifecycle.LifeLogger(600): onCreateView event 06-11
07:07:10.043: D/com.example.lifecycle.LifeLogger(600): onCreateView
event 06-11 07:07:10.053: D/com.example.lifecycle.LifeLogger(600):
onCreateView event 06-11 07:07:10.063:
D/com.example.lifecycle.LifeLogger(600): onCreateView event 06-11
07:07:10.063: D/com.example.lifecycle.LifeLogger(600): onCreateView
event 06-11 07:07:10.063: D/com.example.lifecycle.LifeLogger(600):
onCreateView event 06-11 07:07:10.063:
D/com.example.lifecycle.LifeLogger(600): onCreateView event 06-11
07:07:10.073: D/com.example.lifecycle.LifeLogger(600): onCreateView
event 06-11 07:07:10.073: D/com.example.lifecycle.LifeLogger(600):
onCreateView event 06-11 07:07:10.083:
D/com.example.lifecycle.LifeLogger(600): onCreateView event 06-11
07:07:10.083: D/com.example.lifecycle.LifeLogger(600): onCreateView
event 06-11 07:07:10.083: D/com.example.lifecycle.LifeLogger(600):
onCreateView event 06-11 07:07:10.093:
D/com.example.lifecycle.LifeLogger(600): onCreateView event 06-11
07:07:10.093: D/com.example.lifecycle.LifeLogger(600): onCreateView
event 06-11 07:07:10.093: D/com.example.lifecycle.LifeLogger(600):
onCreateView event 06-11 07:07:10.103:
D/com.example.lifecycle.LifeLogger(600): onCreateView event 06-11
07:07:10.113: D/com.example.lifecycle.LifeLogger(600): onCreateView
event 06-11 07:07:10.113: D/com.example.lifecycle.LifeLogger(600):
onCreateView event 06-11 07:07:10.113:
D/com.example.lifecycle.LifeLogger(600): onCreate event 06-11
07:07:10.113: D/com.example.lifecycle.LifeLogger(600): onStart event
06-11 07:07:10.113: D/com.example.lifecycle.LifeLogger(600): onResume
event 06-11 07:07:10.193: D/com.example.lifecycle.LifeLogger(600):
onCreateView event 06-11 07:07:10.223: D/gralloc_goldfish(600):
Emulator without GPU emulation detected. 06-11 07:08:19.633:
D/com.example.lifecycle.LifeLogger(600): onPause event 06-11
07:08:20.213: D/com.example.lifecycle.LifeLogger(600): onStop event
06-11 07:08:31.993: D/com.example.lifecycle.LifeLogger(600): onRestart
event 06-11 07:08:31.993: D/com.example.lifecycle.LifeLogger(600):
onStart event 06-11 07:08:31.993:
D/com.example.lifecycle.LifeLogger(600): onResume event 06-11
07:08:51.073: D/com.example.lifecycle.LifeLogger(600): onPause event
06-11 07:08:52.963: D/com.example.lifecycle.LifeLogger(600): onStop
event 06-11 07:08:54.043: D/com.example.lifecycle.LifeLogger(600):
onDestroy event
What happened here? why is onCreateView is called so many times?
What is the timing of onCreateView?
Thanks.
update:
the xml inflated:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
the manifast:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lifecycle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.lifecycle.LifeLogger"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
run on android api level 16
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…