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

android Activity生命周期的例子的更多相关文章

  1. [转]: 两分钟彻底让你明白Android Activity生命周期(图文)!

    转自:http://blog.csdn.net/android_tutor/article/details/5772285 大家好,今天给大家详解一下Android中Activity的生命周期,我在前 ...

  2. Android Activity 生命周期详解

    学习android开发这么久对于activity的生命周期还没有仔细思考过,所以,我大致的把这些东西整理一下,希望通过这使自己理解的更透彻点吧! 首先看一下Activity生命周期图和它的的四个阶段 ...

  3. Android Activity生命周期

    从android api文档摘抄出来的activity生命周期图如下: Activity有如下四种状态 a.活动状态  activity处于屏幕前台,获取到了焦点可以和用户进行交互,同一时刻只有一个a ...

  4. Android Activity生命周期详讲

    管理 Activity 生命周期 通过实现回调方法管理 Activity 的生命周期对开发强大而又灵活的应用至关重要. Activity 的生命周期会直接受到 Activity 与其他 Activit ...

  5. android Activity生命周期(设备旋转、数据恢复等)与启动模式

    1.Activity生命周期     接下来将介绍 Android Activity(四大组件之一) 的生命周期, 包含运行.暂停和停止三种状态,onCreate.onStart.onResume.o ...

  6. Android Activity生命周期以及Fragment生命周期的区别与分析

    Android Fragment生命周期图: Activity生命周期图: 对照图: Fragment生命周期分析: 1. 当一个fragment被创建的时候,它会经历以下状态. onAttach() ...

  7. xamarin Android activity生命周期详解

    学Xamarin我为什么要写这样一篇关于Android 的activity生命周期的文章 已经学Xamarin android有一段时间了,现在想起当初Xamarin也走了不少的弯路.当然Xamari ...

  8. Android——Activity生命周期(转)

    Activity生命周期   子曰:溫故而知新,可以為師矣.<論語> 学习技术也一样,对于技术文档或者经典的技术书籍来说,指望看一遍就完全掌握,那基本不大可能,所以我们需要经常回过头再仔细 ...

  9. Android Activity生命周期的几个问题

      每一个Android开发者都应该知道,android系统有四个重要的基本组件,即Activity(活动).Service(服务).Broadcast Receive(广播接收器)和Content ...

随机推荐

  1. HDU - 4802 - GPA (水题)

    题意: 计算GPA,输入一个数字和一个字符串,用 数字×字符串对应的数值 思路: 用map对应数值,要注意的是字符串为P或者N的时候,不计入结果 代码: #include<iostream> ...

  2. HDU-1241-油藏

    这题一道深搜的简单题目,其实题目的思路就只是向八个方向搜索,然后把整个油田遍历一遍即可. #include <cstdio> #include <cstring> int ma ...

  3. 【dp】拔河比赛

    01背包:感谢ZCK大佬 题目描述 学校举行拔河比赛,所有的人被分成了两组,每个人必须(且只能够)在其中的一组,要求两个组的人数相差不能超过1,且两个组内的所有人体重加起来尽可能地接近. 输入 输入中 ...

  4. (51)zabbix命令:zabbix_get获取item数据

    zabbix_get是什么?有什么作用? 总有人在群里提问,为什么zabbix获取不到数据,为什么zabbix提示Not Support,怎么办?别老问,用zabbix_get试着获取数据即可.在za ...

  5. Web鼠标事件

    mousedown:鼠标按下 mouseup:鼠标抬起 mousemove:鼠标移动 mouseout:在父元素上绑定该事件,当鼠标从父元素或者从子元素上离开时都会触发该事件 mouseleave:和 ...

  6. Django ORM操作及进阶

    一般操作 看专业的官网文档,做专业的程序员! 必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 ...

  7. ACM Changchun 2015 A. Too Rich

    You are a rich person, and you think your wallet is too heavy and full now. So you want to give me s ...

  8. 经典DP问题--poj1088滑雪

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  9. js request学习

    SP的内置对象在JSP页面中无须声明就可以直接使用,其内置对象常用的有Request,response,session,application,out,config,pageCOntext reque ...

  10. centos7 安装nodejs 最新版

    笔者在安装时,node为11.0.0版本.这里以11版本为例,以后更新,安装步骤时一致的. 下载node安装包到指定目录 wget https://npm.taobao.org/mirrors/nod ...