.Net程序员玩转Android开发---(22)Activity生命周期

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=》onDestroy

2.  A 页面启动后,点击主页键: onCreate-》onStart=》 onResume=》点击home键=>onPause=》 onStop

3.  A页面启动后,点击按钮打开B页面,B页面把A页面全部遮挡

            onCreate-》onStart=》 onResume=》点击按钮打开B页面--onPause=》onStop--点击BACK键返回--onRestart=>onstart=>onResume

4. A页面失去焦点,但依然可见,此时处于暂停状态

               onCreate-》onStart=》 onResume=》点击按钮打开B页面--onPause=》点击BACK键返回=》onResume

 上面的这个图片,详细描述了activity的执行流程

     第一种情况是当activity不可见的时候,处于停止状态,执行onstop,再次回到前端的时候然后执行onstart进行重启

     第二种情况是当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">



    <Button
        android: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" />

    <Button
        android: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">

    <TextView
        android: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">

    <TextView
        android: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">

    <application
        android: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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值