onCreateView方法被调用时?多少次活动的生命周期? [英] onCreateView method gets called when? and How many times in Activity life cycle?

查看:145
本文介绍了onCreateView方法被调用时?多少次活动的生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小程序看起来像这样:

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.

在LogCat中是:

七月6日至11日:07:10.033:D / com.example.lifecycle.LifeLogger(600):   onCreateView事件七月6日至11日:07:10.033:   D / com.example.lifecycle.LifeLogger(600):onCreateView事件06-11   07:07:10.043:D / com.example.lifecycle.LifeLogger(600):onCreateView   事件七月6日至11日:07:10.053:D / com.example.lifecycle.LifeLogger(600):   onCreateView事件七月6日至11日:07:10.063:   D / com.example.lifecycle.LifeLogger(600):onCreateView事件06-11   07:07:10.063:D / com.example.lifecycle.LifeLogger(600):onCreateView   事件七月6日至11日:07:10.063:D / com.example.lifecycle.LifeLogger(600):   onCreateView事件七月6日至11日:07:10.063:   D / com.example.lifecycle.LifeLogger(600):onCreateView事件06-11   07:07:10.073:D / com.example.lifecycle.LifeLogger(600):onCreateView   事件七月6日至11日:07:10.073:D / com.example.lifecycle.LifeLogger(600):   onCreateView事件七月6日至11日:07:10.083:   D / com.example.lifecycle.LifeLogger(600):onCreateView事件06-11   07:07:10.083:D / com.example.lifecycle.LifeLogger(600):onCreateView   事件七月6日至11日:07:10.083:D / com.example.lifecycle.LifeLogger(600):   onCreateView事件七月6日至11日:07:10.093:   D / com.example.lifecycle.LifeLogger(600):onCreateView事件06-11   07:07:10.093:D / com.example.lifecycle.LifeLogger(600):onCreateView   事件七月6日至11日:07:10.093:D / com.example.lifecycle.LifeLogger(600):   onCreateView事件七月6日至11日:07:10.103:   D / com.example.lifecycle.LifeLogger(600):onCreateView事件06-11   07:07:10.113:D / com.example.lifecycle.LifeLogger(600):onCreateView   事件七月6日至11日:07:10.113:D / com.example.lifecycle.LifeLogger(600):   onCreateView事件七月6日至11日:07:10.113:   D / com.example.lifecycle.LifeLogger(600):OnCreate事件06-11   07:07:10.113:D / com.example.lifecycle.LifeLogger(600):ONSTART事件   七月六日至11号:07:10.113:D / com.example.lifecycle.LifeLogger(600):onResume   事件七月6日至11日:07:10.193:D / com.example.lifecycle.LifeLogger(600):   onCreateView事件七月6日至11日:07:10.223:D / gralloc_goldfish(600):   仿真器没有GPU模拟检测。七月六日至11号:08:19.633:   D / com.example.lifecycle.LifeLogger(600):在onPause事件06-11   07:08:20.213:D / com.example.lifecycle.LifeLogger(600):的onStop事件   七月六日至11号:08:31.993:D / com.example.lifecycle.LifeLogger(600):onRestart   事件七月6日至11日:08:31.993:D / com.example.lifecycle.LifeLogger(600):   ONSTART事件七月6日至11日:08:31.993:   D / com.example.lifecycle.LifeLogger(600):onResume事件06-11   07:08:51.073:D / com.example.lifecycle.LifeLogger(600):在onPause事件   七月六日至11号:08:52.963:D / com.example.lifecycle.LifeLogger(600):的onStop   事件七月6日至11日:08:54.043:D / com.example.lifecycle.LifeLogger(600):   事件的onDestroy

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

这里发生了什么?为什么onCreateView叫了这么多次?

What happened here? why is onCreateView is called so many times?

什么是onCreateView的时机?

What is the timing of 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>

在manifast:

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>

在Android API级别运行16

run on android api level 16

推荐答案

您已经扩展您的类活动。这意味着你的类的生命周期将是如下。

You have extended your class with Activity. That means your class' lifecycle would be as below.

所以,onCreateView不是一个生命周期方法的活性。这只是其中将用于指定任务在文档所述一个部件的方法。

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.

标准实施   android.view.LayoutInflater.Factory.onCreateView充气时使用   与getSystemService返回的LayoutInflater。本   实现什么也不做,是   pre-android.os.Build.VERSION_ codeS.HONEYCOMB应用程序。较新的应用程序应该   使用onCreateView(查看,字符串,上下文的AttributeSet)。

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).

要依靠onCreateView()在活动通话是坏的编程。

To rely of call of onCreateView() in Activity is bad programming.

如果你使用片段扩展到类和已经写onCreateView()方法。然后,它会得到所谓的只有两次你onAttach()和onDestroyView()后,如果你仍然在同一个片段。

If you were using Fragment extended to your class and have written onCreateView() method. Then it would have got called only twice after your onAttach() and onDestroyView() if you are still on same fragment.

请参阅本图。

下面,它的生命周期的片段的方法。

Here, it's a method of lifecycle for Fragment.

因此​​,如果正在测试用错误的假设。这就是全部!

So, you are testing with wrong assumptions. That's all!

这篇关于onCreateView方法被调用时?多少次活动的生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆