android--Activity的生命周期与激活

         Activity是什么?Activity是可以和用户直接交互的组件,是android四大组件之一。android中的四大组件为:ActivityBroadcast、Receiver、Service、ContentProvider。

Activity生命周期:
生命周期的含义:
Activity运行的某一个特定的时期,这个时期可以完成某些特定任务
主要的方法及含义

这张图是从android文档中引用过来的。

生命周期方法不能手动去调用,自动调用
onCreate Activity创建时调用,一般完成初始化的工作
onStart Activity处于可见状态
onResume 处于前台(可交互)状态,恢复数据工作
onPause 处于后台(不可交互)状态,保存数据(播放进度)工作
onStop 处于不可见状态
onDestroy 处于销毁状态,完成资源释放工作
onRestart 从不可见到可见的中间状态,重新启动
不同情况下生命周期的执行流程
1、打开A界面,然后返回
onCreate---onStart----onResume----onPause---onStop----onDestroy
2、打开A界面,然后按Home,再重新打开A界面
onCreate---onStart----onResume----onPause---onStop---onRestart---onStart---onResume
3、A被B完全覆盖
A onCreate---A onStart----A onResume----A onPause---B onCreate---B onStart----B onResume---A onStop
4、A被B覆盖,但任然可见(对话框模式android:theme="@android:style/Theme.Dialog")
A onCreate---A onStart----A onResume----A onPause---B onCreate---B onStart----B onResume
5、横竖屏切换
把原来的Activity销毁掉,启动一个新的Activity
onPause---onStop----onDestroy---onCreate---onStart----onResume
意图激活新的Activity:
激活Activity分为隐性意图和显性意图两种方法。
显性意图激活:
布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.huse.activity.MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getNewActivity"
        android:text="激活一个新的activity" />

</RelativeLayout>
布局文件activity_other.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    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"
    tools:context="cn.edu.huse.activity.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大家好,我是otherActivity" />

</RelativeLayout>
1、定义一个类继承Activity,实现onCreate方法
package cn.edu.huse.activity;

import android.app.Activity;
import android.os.Bundle;

public class OtherActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_other);
	}
}

2、在Manifest文件进行注册

3、调用startActivity
//按钮事件
	public void getNewActivity(View v){
		//激活OtherActivity
		//创建意图--1
		Intent intent = new Intent(this,OtherActivity.class);
		/*创建意图--2
		 * Intent intent = new Intent();
		  intent.setClass(this, OtherActivity.class);
		  //上下选一
		  intent.setClassName(this,"cn.edu.huse.activity.OtherActivity");
		  */
		
		/*Intent intent = new Intent();
		ComponentName component = new ComponentName(this,OtherActivity.class);
		intent.setComponent(component); //设置组件
		*/
		
		//激活
		startActivity(intent);
		//显性意图激活,能明确的知道激活那个组件
	}
运行结果:



隐性意图激活:(隐性意图激活和显性意图激活前面两个步骤是一样的,先完成前面两个步骤)。
布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="cn.edu.huse.hello.MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getOtherActivity"
        android:text="激活另一个项目的activity" />

</RelativeLayout>
3、对外暴露自己
4、激活
	public void getOtherActivity(View v){
		Intent intent = new Intent();
		//必须和你对外暴露的action是一样的
		intent.setAction("cn.edu.huse.qingliang");
		//这里的前缀必要有一个:,但是在暴露的时候不用写
		intent.setDataAndType(Uri.parse("hello:"), "qq/ll");
		startActivity(intent);
		//隐性意图激活原则:完全匹配对方暴露的intent-filter里面的action、category、data
	}
运行结果:


隐性意图和显性意图虽然都可以激活Activity,但是隐性意图可以激活不是不是一个项目下的Activity,只要你敢暴露,我就敢去用。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值