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

  • 相关阅读:
    XMLHttpRequest 跨域问题
    jQuery+AJAX实现纯js分页功能
    PHP验证码
    PHP基础
    UIView易忽略点
    UITableView  优化
    SpringBoard界面层级结构分析
    给App在“设置”中添加选项(类似招行App)
    通过USB线SSH登陆到越狱手机上(命令行模式的),不通过wifi
    在IOS项目中使用Lua
  • 原文地址:https://www.cnblogs.com/gisoracle/p/5012426.html
Copyright © 2011-2022 走看看