• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

gisoracle

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

android Activity生命周期的例子

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 + "创建:\n";
            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 + "摧毁:\n";
            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 + "暂停:\n";
            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 + "重新启动:\n";
            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 + "继续:\n";
            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 + "开始:\n";
            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 + "停止:\n";
            pTextView.setText(Str);
        } catch (Exception e) {
            //Log.e(TAG, "error : "+e.getMessage(), e);
        }
    }

}

日志如下:

onCreate
onStart
onResume

onPause
onStop
onDestroy

posted on 2015-12-02 11:22  gisai  阅读(303)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3