• 测试Activity和Fragment的生命周期


    Activity的生命周期有7个函数,Fragment的生命周期有11个函数。

    Activity生命周期除上述6个方法还有一个Restart()方法,该方法在该Activity从不可见(仍存在)到重新可见时调用。

    测试代码如下:

    package com.example.asus.animport;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.i("info","Activity--onCreate()");
            setContentView(R.layout.activity_main);
    
            android.app.Fragment tFragment = new TFragment();
            getFragmentManager().beginTransaction().add(R.id.FLayout,tFragment).commit();
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Log.i("info","Activity--onStart()");
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Log.i("info","Activity--onResumed()");
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Log.i("info","Activity--onPause()");
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            Log.i("info","Activity--onStop()");
        }
    
        @Override
        protected void onRestart() {
            super.onRestart();
            Log.i("info","Activity--onRestart()");
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.i("info","Activity--onDestroy()");
        }
    
        public static class TFragment extends android.app.Fragment{
    
            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
                Log.e("info","Fragment--onAttach()");
            }
    
            @Override
            public void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Log.e("info","Fragment--onCreate()");
            }
    
            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                Log.e("info","Fragment--onCreateView()");
                return super.onCreateView(inflater, container, savedInstanceState);
            }
    
            @Override
            public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                Log.e("info","Fragment--onActivityCreated()");
            }
    
            @Override
            public void onStart() {
                super.onStart();
                Log.e("info","Fragment--onStart()");
            }
    
            @Override
            public void onResume() {
                super.onResume();
                Log.e("info","Fragment--onResume()");
            }
    
            @Override
            public void onPause() {
                super.onPause();
                Log.e("info","Fragment--onPause()");
            }
    
            @Override
            public void onStop() {
                super.onStop();
                Log.e("info","Fragment--onStop()");
            }
    
            @Override
            public void onDestroyView() {
                super.onDestroyView();
                Log.e("info","Fragment--onDestroyView()");
            }
    
            @Override
            public void onDestroy() {
                super.onDestroy();
                Log.e("info","Fragment--onDestroy()");
            }
    
            @Override
            public void onDetach() {
                super.onDetach();
                Log.e("info","Fragment--onDetach()");
            }
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.asus.animport.MainActivity">
    
        <FrameLayout
            android:id="@+id/FLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </LinearLayout>

    运行程序:

    切换到其他应用:

    切换回本应用:

    退出应用:

    可以看出:各过程的运行结果与第一张图Activity与Fragment生命周期的方法对应一致,特别需要注意的是,由于Fragment依赖Activity的存在而存在,故在创建时Activity生命周期中的方法均先于Fragment生命周期中的方法执行;相反,在销毁时,是先执行Fragment生命周期中的方法再执行Activity生命周期中的方法

    (参考资料:http://www.2cto.com/kf/201512/453242.htmlhttp://blog.csdn.net/zjclugger/article/details/10442335

    值得一提的是,Fragment生命周期中的方法onAttach(Context context)在执行该程序中并没有执行,查阅资料发现在API低于23的版本中不会执行该方法,而是执行onAttach(Activity Activity)。(参考资料:http://blog.csdn.net/cadi2011/article/details/53314055

  • 相关阅读:
    (转)Quick Tip: Provisioning Web Parts to a Page(添加web part到page上,)
    解决sharepoint 2010浏览器在线浏览Word出错(非原创)
    转:部署带webpart的网页(Deploy a Page using a Feature)
    单点登录到sharepoint
    Feature Base On WebApp
    Create a New Document Using Office Web Apps
    学校合并与数组合并
    DNN拟合曲线
    使用tf.keras.layers.Layer自定义神经网络的层
    numpy
  • 原文地址:https://www.cnblogs.com/LangZXG/p/6501839.html
Copyright © 2020-2023  润新知