use*_*910 27 lifecycle android logcat
我写了一个看起来像这样的小程序:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
这是主要的活动.
LogCat是:
06-11 07:07:10.033:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.033:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06- 11 07:07:10.043:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.053:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07 :07:10.063:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.063:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07 :10.063:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.063:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.073 :D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.073:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.083:D /com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.083:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.083:D/com .example.lifecy cle.LifeLogger(600):onCreateView事件06-11 07:07:10.093:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.093:D/com.example.lifecycle. LifeLogger(600):onCreateView事件06-11 07:07:10.093:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.103:D/com.example.lifecycle.LifeLogger( 600):onCreateView事件06-11 07:07:10.113:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06-11 07:07:10.113:D/com.example.lifecycle.LifeLogger(600) :onCreateView事件06-11 07:07:10.113:D/com.example.lifecycle.LifeLogger(600):onCreate事件06-11 07:07:10.113:D/com.example.lifecycle.LifeLogger(600):onStart事件06-11 07:07:10.113:D/com.example.lifecycle.LifeLogger(600):onResume事件06-11 07:07:10.193:D/com.example.lifecycle.LifeLogger(600):onCreateView事件06 -11 07:07:10.223:d/gralloc_goldfish(600):仿真器,而不GPU仿真检测.06-11 07:08:19.633:D/com.example.lifecycle.LifeLogger(600):onPause事件06-11 07:08:20.213:D/com.example.lifecycle.LifeLogger(600):onStop事件06- 11 07:08:31.993:D/com.example.lifecycle.LifeLogger(600):onRestart事件06-11 07:08:31.993:D/com.example.lifecycle.LifeLogger(600):onStart事件06-11 07 :08:31.993:D/com.example.lifecycle.LifeLogger(600):onResume事件06-11 07:08:51.073:D/com.example.lifecycle.LifeLogger(600):onPause事件06-11 07:08 :52.963:D/com.example.lifecycle.LifeLogger(600):onStop事件06-11 07:08:54.043:D/com.example.lifecycle.LifeLogger(600):onDestroy事件
这里发生了什么?为什么onCreateView被调用了这么多次?
onCreateView的时机是什么时候?
谢谢.
更新:
xml膨胀:
<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>
Run Code Online (Sandbox Code Playgroud)
表现:
<?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 Code Online (Sandbox Code Playgroud)
在android api 16级上运行
MKJ*_*ekh 49
你已经扩展了你的课程Activity.这意味着您班级的生命周期如下.

因此,onCreateView不是活动的生命周期方法.它只是一个成员方法,将用于doc中所述的指定任务.
与getSystemService返回的LayoutInflater充气时,标准实施android.view.LayoutInflater.Factory.onCreateView的使用.此实现不执行任何操作,适用于pre-android.os.Build.VERSION_CODES.HONEYCOMB应用程序.较新的应用程序应使用onCreateView(View,String,Context,AttributeSet).
依赖于Activity中onCreateView()的调用是不好的编程.
如果你使用Fragment扩展到你的类并且编写了onCreateView()方法,那么如果你仍在同一个片段上,它只会在你的onAttach()和onDestroyView()之后被调用两次.
见这个图.

在这里,它是Fragment的生命周期方法.
所以,你正在测试错误的假设.就这样!
| 归档时间: |
|
| 查看次数: |
35435 次 |
| 最近记录: |