在android中有两个生命周期--基本上是一个活动生命周期和一个视图生命周期( View lifecycle )--但是在活动生命周期的哪个阶段启动视图Lifecycle?,从视图生命周期开始的那一刻起,它是一直贯穿于它的生命周期,还是在活动生命周期的各个部分上完成?(如果是,请说明在哪个部分调用了哪些方法)
发布于 2020-02-25 12:53:35
这里不是一个100%的官方图表,我是从正式的活动生命周期图和视图生命周期中混合而来的,因为我在安卓文档中没有看到任何关于视图生命周期的图表(如果我们可以称之为生命周期)。
因此,在图表中,添加橙色箭头是为了观察以下演示应用程序,该应用程序只是为了演示两个生命周期之间的关系;其中我创建了一个虚拟自定义TextView
,并记录了它的超类生命周期方法及其保持活动生命周期方法;并相应地添加了橙色箭头。
因此,与图中一样,活动是由其onCreate()
创建的,在活动的布局被setContentView()
膨胀之后,系统调用布局视图的构造函数来构造它们。
现在,活动刚刚创建,视图的实例被构建;屏幕上仍然什么也看不见;因此,活动的onStart()
& onResume()
方法分别被调用,因此该活动现在在屏幕上可见;仍然没有在屏幕上绘制任何内容。
然后,当活动的窗口被附加到窗口管理器时,该活动的onAttachedToWindow()
被调用,向要在活动上绘制的底层视图发出绿灯。
此时,活动的生命被认为处于运行/活动状态。按照活动的onAttachedToWindow()
,当视图附加到活动的窗口时,将调用视图的onAttachedToWindow()
,现在视图可以开始在屏幕上绘图。
在屏幕上绘制视图通过一系列调用来确定一些度量,如尺寸和属性;诸如宽度、高度、颜色、文本、填充等。这需要调用measure()
几次;随后调用onMeasure()
最后,调用onDraw()
,它有一个画布参数,用作绘制视图的目标面。
您可以找到在图形上描述的其他绘图相关方法,并且可以在这里中深入研究它们。
使用onDestroy()
销毁活动时,在活动的主窗口与窗口管理器分离之前,底层视图将首先利用机会从活动的窗口中分离出来;在此情况下,将调用视图的onDetachedFromWindow()
来声明它已从屏幕上删除,因此该活动现在可以与窗口管理器分离;当发生这种情况时,将相应地调用该活动的onDetachedFromWindow()
。
因此,除非首先将所有视图从窗口中分离,否则活动不会从窗口管理器中分离。
因此,在视图中没有像onDestroy()
这样的东西,视图可以附加或脱离活动的窗口;您可以检查这答案。此外,视图的onDetachedFromWindow()
与其活动的onDestroy()
高度耦合,如果调用活动的onStop()
,视图仍然附加到活动的窗口。
所以,没有什么东西我们可以称之为视图被破坏,但一个视图是分离的。
还请注意,活动和视图的onAttachedToWindow()
与活动的onCreate()
非常耦合;因此,当调用活动的onRestart()
时,活动及其视图已经与窗口相关联,因此onAttachedToWindow()
没有被调用(在图上这一点不太清楚)。
类似地,视图的onDetachedFromWindow()
&活动仅与onDestroy()
绑定;因此,当活动暂停或停止时,onDetachedFromWindow()
不会调用,因此视图仍然附加到窗口。
您还可以使用parentLayout.removeView(customView);
并观察回调。
下面是用于进行此观察的演示示例
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<com.example.android.androidxtest.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/long_text"
android:textSize="22sp" />
</LinearLayout>
活性
public class LifeCycleActivity extends AppCompatActivity {
private static final String TAG = "LOG_TAG_ACTIVITY";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
setContentView(R.layout.activity_view_lifecycle);
Log.d(TAG, "onCreate: after setContentView()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart: ");
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Log.d(TAG, "onAttachedToWindow: ");
}
@Override
public boolean isDestroyed() {
Log.d(TAG, "isDestroyed: ");
return super.isDestroyed();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
Log.d(TAG, "onDetachedFromWindow: ");
}
}
自定义视图
public class CustomTextView extends TextView {
private static final String TAG = "LOG_TAG_VIEW";
public CustomTextView(Context context) {
super(context);
Log.d(TAG, "CustomTextView: Constructor");
}
public CustomTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
Log.d(TAG, "CustomTextView: Constructor");
}
public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.d(TAG, "CustomTextView: Constructor");
}
public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
Log.d(TAG, "CustomTextView: Constructor");
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.d(TAG, "onAttachedToWindow: ");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d(TAG, "onMeasure: ");
}
@Override
public void layout(int l, int t, int r, int b) {
super.layout(l, t, r, b);
Log.d(TAG, "layout: ");
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Log.d(TAG, "onLayout: ");
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
Log.d(TAG, "dispatchDraw: ");
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Log.d(TAG, "draw: ");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d(TAG, "onDraw: ");
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Log.d(TAG, "onDetachedFromWindow: ");
}
}
登录应用程序启动
2020-02-25 14:09:41.859 2043-2043/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onCreate:
2020-02-25 14:09:41.945 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: CustomTextView: Constructor
2020-02-25 14:09:41.945 2043-2043/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onCreate: after setContentView()
2020-02-25 14:09:41.947 2043-2043/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onStart:
2020-02-25 14:09:41.954 2043-2043/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onResume:
2020-02-25 14:09:41.984 2043-2043/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onAttachedToWindow:
2020-02-25 14:09:41.985 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onAttachedToWindow:
2020-02-25 14:09:41.993 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onMeasure:
2020-02-25 14:09:42.005 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onMeasure:
2020-02-25 14:09:42.006 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onLayout:
2020-02-25 14:09:42.006 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: layout:
2020-02-25 14:09:42.032 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onDraw:
2020-02-25 14:09:42.032 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: dispatchDraw:
2020-02-25 14:09:42.032 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: draw:
登录菜单按钮(应用程序的历史记录)
2020-02-25 13:44:44.462 32357-32357/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onPause:
2020-02-25 13:44:44.511 32357-32357/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onStop:
从菜单按钮(应用程序的历史记录)登录回我们的应用程序
2020-02-25 14:11:23.387 2043-2043/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onRestart:
2020-02-25 14:11:23.392 2043-2043/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onStart:
2020-02-25 14:11:23.394 2043-2043/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onResume:
2020-02-25 14:11:23.405 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onMeasure:
2020-02-25 14:11:23.420 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onLayout:
2020-02-25 14:11:23.420 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: layout:
2020-02-25 14:11:23.424 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onDraw:
2020-02-25 14:11:23.424 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: dispatchDraw:
2020-02-25 14:11:23.424 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: draw:
2020-02-25 14:11:23.455 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onMeasure:
2020-02-25 14:11:23.460 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onMeasure:
2020-02-25 14:11:23.460 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onLayout:
2020-02-25 14:11:23.460 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: layout:
2020-02-25 14:11:23.461 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: onDraw:
2020-02-25 14:11:23.461 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: dispatchDraw:
2020-02-25 14:11:23.462 2043-2043/com.example.android.androidxtest D/LOG_TAG_VIEW: draw:
登录配置更改(旋转)
2020-02-25 17:05:00.481 8058-8058/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onPause:
2020-02-25 17:05:00.492 8058-8058/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onStop:
2020-02-25 17:05:00.493 8058-8058/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onDestroy:
2020-02-25 17:05:00.512 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: onDetachedFromWindow:
2020-02-25 17:05:00.517 8058-8058/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onDetachedFromWindow:
2020-02-25 17:05:00.563 8058-8058/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onCreate:
2020-02-25 17:05:00.600 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: CustomTextView: Constructor
2020-02-25 17:05:00.601 8058-8058/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onCreate: after setContentView()
2020-02-25 17:05:00.604 8058-8058/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onStart:
2020-02-25 17:05:00.611 8058-8058/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onResume:
2020-02-25 17:05:00.626 8058-8058/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onAttachedToWindow:
2020-02-25 17:05:00.626 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: onAttachedToWindow:
2020-02-25 17:05:00.629 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: onMeasure:
2020-02-25 17:05:00.659 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: onMeasure:
2020-02-25 17:05:00.659 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: onLayout:
2020-02-25 17:05:00.660 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: layout:
2020-02-25 17:05:00.674 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: onDraw:
2020-02-25 17:05:00.674 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: dispatchDraw:
2020-02-25 17:05:00.674 8058-8058/com.example.android.androidxtest D/LOG_TAG_VIEW: draw:
背压式
2020-02-25 16:10:24.743 7314-7314/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onPause:
2020-02-25 16:10:25.341 7314-7314/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onStop:
2020-02-25 16:10:25.343 7314-7314/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onDestroy:
2020-02-25 16:10:25.343 7314-7314/com.example.android.androidxtest D/LOG_TAG_VIEW: onDetachedFromWindow:
2020-02-25 16:10:25.344 7314-7314/com.example.android.androidxtest D/LOG_TAG_ACTIVITY: onDetachedFromWindow:
发布于 2020-02-25 02:58:00
每个android组件都有自己的生命周期。服务,查看,碎片,活动,..。我建议您首先使用Android文档来获得它的基本概念。
发布于 2020-02-25 04:22:49
视图生命周期不影响活动生命周期。和其他类型的生命周期是一样的。视图的生命周期只有在视图被添加到屏幕上时才会启动。
https://stackoverflow.com/questions/60386703
复制相似问题