zoukankan      html  css  js  c++  java
  • android Activity生命周期的例子

    package com.example.yanlei.yl2;
    
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnKeyListener;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        public static final String TAG = "生命周期:";
        String Str = "";
        TextView pTextView = null;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            //当创建此Activity的时候回调
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.e(TAG, "onCreate");
            try {
                Str = Str + "创建:
    ";
                pTextView.setText(Str);
            } catch (Exception e) {
                //Log.e(TAG, "error : "+e.getMessage(), e);
            }
    
        }
    
        @Override
        protected void onDestroy() {
            //当销毁此Activity的时候回调
            super.onDestroy();
            Log.e(TAG, "onDestroy");
    
    
            try {
                Str = Str + "摧毁:
    ";
                pTextView.setText(Str);
            } catch (Exception e) {
                //Log.e(TAG, "error : "+e.getMessage(), e);
            }
        }
    
        @Override
        protected void onPause() {
            //当暂停此Activity的时候回调
            super.onPause();
            Log.e(TAG, "onPause");
            try {
                Str = Str + "暂停:
    ";
                pTextView.setText(Str);
            } catch (Exception e) {
                // Log.e(TAG, "error : "+e.getMessage(), e);
            }
        }
    
        @Override
        protected void onRestart() {
            //当重新开始此Activity的时候回调
            super.onRestart();
            Log.e(TAG, "onRestart");
            try
    
            {
                Str = Str + "重新启动:
    ";
                pTextView.setText(Str);
            } catch (Exception e) {
                //Log.e(TAG, "error : "+e.getMessage(), e);
            }
        }
    
        @Override
        protected void onResume() {
            //当显示展示此Activity的界面的时候回调
            super.onResume();
            Log.e(TAG, "onResume");
            try {
                Str = Str + "继续:
    ";
                pTextView.setText(Str);
            } catch (Exception e) {
                //Log.e(TAG, "error : "+e.getMessage(), e);
            }
        }
    
        @Override
        protected void onStart() {
            //当使用此Activity可以接受用户操作的时候回调
            super.onStart();
            Log.e(TAG, "onStart");
            try {
                Str = Str + "开始:
    ";
                pTextView.setText(Str);
            } catch (Exception e) {
                //Log.e(TAG, "error : "+e.getMessage(), e);
            }
        }
    
        @Override
        protected void onStop() {
            //当停止此Activity的时候回调
            super.onStop();
            Log.e(TAG, "onStop");
            try {
                Str = Str + "停止:
    ";
                pTextView.setText(Str);
            } catch (Exception e) {
                //Log.e(TAG, "error : "+e.getMessage(), e);
            }
        }
    
    }

    日志如下:

    onCreate
    onStart
    onResume

    onPause
    onStop
    onDestroy

  • 相关阅读:
    旧题复习{6}
    CF219D. Choosing Capital for Treeland [树形DP]
    POJ1947 Rebuilding Roads[树形背包]

    洛谷P1280 尼克的任务[DP]
    NOIP2003pj栈[卡特兰数]
    NOIP2001统计单词个数[序列DP]
    洛谷P1415 拆分数列[序列DP 状态 打印]
    POJ2828 Buy Tickets[树状数组第k小值 倒序]
    CF380C. Sereja and Brackets[线段树 区间合并]
  • 原文地址:https://www.cnblogs.com/gisoracle/p/5012426.html
Copyright © 2011-2022 走看看