user3728910 user3728910 - 2 months ago 9
Android Question

onCreateView method gets called when? and How many times in Activity life cycle?

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

Answer

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

enter image description here

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.

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

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

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.

See this diagram.

enter image description here

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

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

Comments