Android onCreateView 方法在什么时候被调用?以及 Activity 生命周期中有多少次?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24156926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onCreateView method gets called when? and How many times in Activity life cycle?
提问by user3728910
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:
LogCat 是:
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
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。
What happened here? why is onCreateView is called so many times?
这里发生了什么?为什么 onCreateView 被调用了这么多次?
What is the timing of onCreateView?
onCreateView 的时间是什么?
Thanks.
谢谢。
update:
更新:
the xml inflated:
膨胀的 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>
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
在 android api 级别 16 上运行
回答by MKJParekh
You have extended your class with Activity
. That means your class' lifecycle would be as below.
您已使用Activity
. 这意味着您班级的生命周期如下所示。
So, onCreateView is not a lifecycle method for activity. It's just a member method which will be used for specified tasks as said in doc.
因此,onCreateView 不是活动的生命周期方法。它只是一个成员方法,将用于指定的任务,如文档中所述。
Standard implementation of android.view.LayoutInflater.Factory.onCreateView used when inflating with the LayoutInflater returned by getSystemService. This implementation does nothing and is for pre-android.os.Build.VERSION_CODES.HONEYCOMB apps. Newer apps should use onCreateView(View, String, Context, AttributeSet).
使用 getSystemService 返回的 LayoutInflater 进行充气时使用的 android.view.LayoutInflater.Factory.onCreateView 的标准实现。此实现什么都不做,适用于 android.os.Build.VERSION_CODES.HONEYCOMB 之前的应用程序。较新的应用程序应该使用 onCreateView(View, String, Context, AttributeSet)。
To rely on the call of onCreateView() in an Activity is bad programming.
在 Activity 中依赖 onCreateView() 的调用是糟糕的编程。
If you were using Fragment
extended to your class and have written onCreateView() method, then it would have been called only twice after your onAttach() and onDestroyView() if you are still on same fragment.
如果您使用Fragment
扩展到您的类并编写了 onCreateView() 方法,那么如果您仍在同一个片段上,它只会在 onAttach() 和 onDestroyView() 之后被调用两次。
See this diagram.
看这个图。
Here, it's a method of lifecycle for Fragment.
在这里,它是 Fragment 的生命周期方法。
So, you are testing with wrong assumptions. That's all!
因此,您正在使用错误的假设进行测试。就这样!
回答by Ashwin N Bhanushali
Android Framework uses mechanism of Dependencies injection When layout file is inflated.I think due to this onCreateView is called so many times.Formula for this might be as below
Android 框架使用依赖注入机制 当布局文件膨胀时。我认为由于这个 onCreateView 被调用了很多次。为此的公式可能如下
- no of view in layout xml == no of calls to onCreateView
- 布局 xml 中没有视图 == 没有对 onCreateView 的调用
Try to remove setContentView and see how many times onCreateView is called.You might get some insights into it.
尝试删除 setContentView 并查看 onCreateView 被调用了多少次。您可能会对此有所了解。
回答by Emre Kilinc Arslan
You can monitor the reason onCreateView gets Called from a log file :
您可以监控从日志文件中调用 onCreateView 的原因:
add inside your onCreateView method this :
在您的 onCreateView 方法中添加:
Log.d("TAG", "onCreateView event : " + name);
such as my logcat produce this ;
比如我的 logcat 产生这个;
onCreateView : LinearLayout
onCreateView : ViewStub
onCreateView : FrameLayout
onCreateView : android.support.v7.widget.ActionBarOverlayLayout
onCreateView : android.support.v7.widget.ContentFrameLayout
onCreateView : android.support.v7.widget.ActionBarContainer
onCreateView : android.support.v7.widget.Toolbar
onCreateView : android.support.v7.widget.ActionBarContextView
onCreateView : LinearLayout
回答by Nickolay Savchenko
onCreateView
calls = number of views in layout (create each view)
onCreateView
调用 = 布局中的视图数(创建每个视图)