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
android Activity生命周期的例子的更多相关文章
- [转]: 两分钟彻底让你明白Android Activity生命周期(图文)!
转自:http://blog.csdn.net/android_tutor/article/details/5772285 大家好,今天给大家详解一下Android中Activity的生命周期,我在前 ...
- Android Activity 生命周期详解
学习android开发这么久对于activity的生命周期还没有仔细思考过,所以,我大致的把这些东西整理一下,希望通过这使自己理解的更透彻点吧! 首先看一下Activity生命周期图和它的的四个阶段 ...
- Android Activity生命周期
从android api文档摘抄出来的activity生命周期图如下: Activity有如下四种状态 a.活动状态 activity处于屏幕前台,获取到了焦点可以和用户进行交互,同一时刻只有一个a ...
- Android Activity生命周期详讲
管理 Activity 生命周期 通过实现回调方法管理 Activity 的生命周期对开发强大而又灵活的应用至关重要. Activity 的生命周期会直接受到 Activity 与其他 Activit ...
- android Activity生命周期(设备旋转、数据恢复等)与启动模式
1.Activity生命周期 接下来将介绍 Android Activity(四大组件之一) 的生命周期, 包含运行.暂停和停止三种状态,onCreate.onStart.onResume.o ...
- Android Activity生命周期以及Fragment生命周期的区别与分析
Android Fragment生命周期图: Activity生命周期图: 对照图: Fragment生命周期分析: 1. 当一个fragment被创建的时候,它会经历以下状态. onAttach() ...
- xamarin Android activity生命周期详解
学Xamarin我为什么要写这样一篇关于Android 的activity生命周期的文章 已经学Xamarin android有一段时间了,现在想起当初Xamarin也走了不少的弯路.当然Xamari ...
- Android——Activity生命周期(转)
Activity生命周期 子曰:溫故而知新,可以為師矣.<論語> 学习技术也一样,对于技术文档或者经典的技术书籍来说,指望看一遍就完全掌握,那基本不大可能,所以我们需要经常回过头再仔细 ...
- Android Activity生命周期的几个问题
每一个Android开发者都应该知道,android系统有四个重要的基本组件,即Activity(活动).Service(服务).Broadcast Receive(广播接收器)和Content ...
随机推荐
- 121. Best Time to Buy and Sell Stock@python
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Redis数据库(二)
1. Redis数据库持久化 redis提供了两种数据备份方式,一种是RDB,另外一种是AOF,以下将详细介绍这两种备份策略. 面试: 1.1 配置文件详解备份方式 [root@localhost ...
- python数据类型、字符编码、文件处理-练习
练习-字符串 # 写代码,有如下变量,请按照要求实现每个功能 (共6分,每小题各0.5分) name = " aleX" # ) 移除 name 变量对应的值两边的空格,并输出处理 ...
- css3-flex-box(2)
使用方法 使用Flexbox布局只要在父容器元素上设置display属性: .flex-container { display: -webkit-flex; /* Safari */ display: ...
- 模块导入及使用,关键字,模块搜索路径,python文件的两种用途
06.05自我总结 一.模块导入及使用 1.模块导入的两种方式 我们拿time模块并使用其中的time功能进行举例 a)第一种 import time print(time.time) import首 ...
- 浅谈Session与Cookie的关系
一.概念理解: 首先cookie是服务端识别客户的唯一标识的依据,客户在访问网站时候,服务端为了记住这个客户,会在服务端按照它的规则制作一个cookie数据,会将这个cookie数据保留在服务端一段时 ...
- Oracle中创建触发器示例及注意事项
1.oracle 中创建触发器示例 CREATE TABLE "CONCEPT"."FREQUENCYMODIFYLOG" ( "FREQUENCYI ...
- HDU 3271 SNIBB
SNIBB Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 3271 ...
- BNUOJ 5227 Max Sum
Max Sum Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: ...
- TOJ 假题之 Cow Brainiacs
1570: Cow Brainiacs Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: ...