.Net程序员玩转Android开发---(22)Activity生命周期
2016-12-20 17:47
351 查看
1.介绍
activity是安卓中的重要组件,页面布局的所有操作,都要通过activity来实现。android的activity生命周期主要包括以下几个onCreate 初始化onStart 开始被用户可见onResume 开始可以与用户进行交互onPaused 暂停onStop 停止onDestory 销毁onRestart 重启activity主要有四种状态1.运行状态:当activity获得焦点,处于屏幕最顶端,此时处于运行状态2.暂停状态:当Activity失去了焦点但仍然对用于可见(如栈顶的Activity是透明的或者栈顶Activity并不是铺满整个手机屏幕),此时处于暂停状态;3.停止状态:当Activity被其他Activity完全遮挡,此时此Activity对用户不可见,此时处于停止状态;4.销毁状态:当Activity由于人为或系统原因(如低内存等)被销毁,此时处于销毁状态;下面这四种情况,演示了状态切换1. A页面启动后,点击返回键: onCreate-》onStart=》 onResume=》点击返回键=>onPause=》 onStop=》onDestroy2. A 页面启动后,点击主页键: onCreate-》onStart=》 onResume=》点击home键=>onPause=》 onStop3. A页面启动后,点击按钮打开B页面,B页面把A页面全部遮挡 onCreate-》onStart=》 onResume=》点击按钮打开B页面--onPause=》onStop--点击BACK键返回--onRestart=>onstart=>onResume4. A页面失去焦点,但依然可见,此时处于暂停状态 onCreate-》onStart=》 onResume=》点击按钮打开B页面--onPause=》点击BACK键返回=》onResume 上面的这个图片,详细描述了activity的执行流程 第一种情况是当activity不可见的时候,处于停止状态,执行[b]onstop,再次回到前端的时候然后执行onstart进行重启[/b] 第二种情况是当activity处于可见状态,但失去焦点后,转向onpause状态,再次回到前端的时候执行onresume事件 第三种情况是当系统处于onpause或者onstop的时候,系统内存不足的时候,会强制杀死占用的内存,再次回到前端,会执行oncreate进行初始创建2.示例
下面的示例演示了activity的各个状态的执行情况,主页面上有两个按钮,打开透明页面和打开全屏页面. 打开透明页面按钮,打开一个新页面后,原页面仍然可见。 activity_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:background="@color/colorPrimaryDark"tools:context="com.example.app.appdemo.MainActivity"android:weightSum="1"><Buttonandroid:text="打开透明页面"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_marginTop="29dp"android:id="@+id/btn1" /><Buttonandroid:text="打开全屏页面"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/button"android:id="@+id/btn2" /></LinearLayout>MainActivity.java
package com.example.app.appdemo; import android.content.Intent; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private Button button1; private Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //onCreate是activity的入口,所有activity都是通过onCreate初始化的 initView(); Log.i(TAG, "onCreate: 页面创建"); } private void initView() { button1=(Button) findViewById(R.id.btn1);//打开透明页面 button2=(Button) findViewById(R.id.btn2);//打开全屏 button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,FirstActivity.class); startActivity(intent); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); } @Override protected void onStart() { Log.i(TAG, "onStart: 页面启动"); super.onStart(); } @Override protected void onResume() { Log.i(TAG, "onResume: 页面恢复"); super.onResume(); } @Override protected void onRestart() { Log.i(TAG, "onRestart: 页面重启"); super.onRestart(); } @Override protected void onPause() { Log.i(TAG, "onPause: 页面暂停"); super.onPause(); } @Override protected void onStop() { Log.i(TAG, "onStop: 页面停止"); super.onStop(); } @Override protected void onDestroy() { Log.i(TAG, "onDestroy: 页面销毁"); super.onDestroy(); } }
activity_first.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="透明页面"/></LinearLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="全屏页面"/></LinearLayout>AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.app.appdemo"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".FirstActivity"android:theme="@android:style/Theme.Translucent"></activity><activity android:name=".SecondActivity"></activity></application></manifest>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件