代码之家  ›  专栏  ›  技术社区  ›  juztcode

android的生命周期重叠

  •  0
  • juztcode  · 技术社区  · 5 年前

    查看生命周期启动?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Zain    5 年前

    activity life cycle 图表和 view life cycle

    enter image description here

    因此,在图中,橙色箭头是在观察下面的演示应用程序时添加的,这只是为了演示两个生命周期之间的关系;在这里,我创建了一个虚拟的自定义 TextView

    因此,如图中所示,活动是由 onCreate() 在活动的布局被 setContentView()

    现在,活动刚刚创建,视图的实例已经构建;屏幕上仍然没有可见的内容;因此,活动的 onStart() onResume() 方法被分别调用,因此活动现在在屏幕上可见;但仍然没有在上面绘制任何内容。

    然后,当活动的窗口附加到窗口管理器时,活动的 onAttachedToWindow() 称为,向要在活动上绘制的基础视图亮绿灯。

    后续活动 在连接窗口() ,视图是 onAttachedToWindow() 当它附加到活动的窗口时调用,现在视图可以开始在屏幕上绘制。

    在屏幕上绘制视图会经过一系列调用,以确定一些度量值,如大小和属性;如宽度、高度、颜色、文本、填充等。。这需要打电话 measure() onMeasure() 被称为

    最终, onDraw()

    您可以找到图中描述的其他绘图关联方法,并可以深入到 here .

    当活动被破坏时 onDestroy() ,然后,在活动的主窗口与窗口管理器分离之前,基础视图将首先有机会与活动的窗口分离;当这一操作到位时,视图的 onDetachedFromWindow() 调用以声明它已从屏幕中移除,因此活动现在可以从窗口管理器中分离;当发生这种情况时,活动的 onDetachedFromWindow() 将相应地调用。

    因此,除非首先将所有视图与窗口分离,否则活动不会与窗口管理器分离。

    所以,没有什么东西像 ,视图可以附加或从活动窗口分离;您可以检查 this 回答这个问题。此外 onDestroy() ,如果活动是 onStop() 调用时,视图仍附加到活动的窗口。

    所以,没有这样的东西,我们可以称之为一个视图被破坏,但一个视图是分离的。

    在连接窗口() 的活动视图与 所以当 onRestart() 活动的视图已与窗口关联,因此 不被调用(这在图表上不太清楚)。

    的视图和活动仅与 ;因此,当活动暂停或停止时 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: 
    
        2
  •  0
  •   Thành Hà Văn    5 年前

    我建议您先访问Android文档,了解它的基本概念。

        3
  •  0
  •   Jian Astrero    5 年前

    视图生命周期不会干扰活动生命周期。与其他类型的生命周期一样。视图生命周期开始的唯一时间是视图添加到屏幕时。