【移动应用开发技术】Android Activity生命周期_第1页
【移动应用开发技术】Android Activity生命周期_第2页
【移动应用开发技术】Android Activity生命周期_第3页
【移动应用开发技术】Android Activity生命周期_第4页
【移动应用开发技术】Android Activity生命周期_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】AndroidActivity生命周期

Android—Activity生命周期一、图解

二、概念分析

当一个活动被创建时,调用onCreate()

方法当一个活动能被用户看到(即可见)时,调用onStart()

方法当一个活动可获取用户焦点时,调用onResume()

方法所以打开一个活动时,会先后执行onCreate()->onStart()->onResume三个方法

当一个活动不能获取用户焦点时(此时另一个活动进入前台),调用onPause()

方法当一个活动不可见(即被其他活动完全覆盖)时,调用onStop()

方法当我们准备启动另一个新的活动覆盖当前活动时时,会依次调用当前活动类的

onPause()

->

onStop()

方法

当我们再次启动该活动,该活动由后台转到前台运行时,会依次调用该活动类的onRestart()->onStart()->onResume()三个方法

当我们退出程序,或调用finish()

方法时,则依次调用

onPause()

->

onStop()

->

onDestroy()

方法

三、例子说明

//FirstActivity.javapackage

mars.activity05;

import

android.app.Activity;import

android.content.Intent;import

android.os.Bundle;import

android.view.View;import

android.view.View.OnClickListener;import

android.widget.Button;

public

class

FirstActivity

extends

Activity

{/**

Called

when

the

activity

is

first

created.

*/private

Button

myButton;

@Overridepublic

void

onCreate(Bundle

savedInstanceState)

{System.out.println("FirstActivity

>

onCreate

");super.onCreate(savedInstanceState);setContentView(R.layout.main);myButton

=

(Button)

findViewById(R.id.myButton);myButton.setOnClickListener(new

ButtonListener());}

@Overrideprotected

void

onDestroy()

{//

TODO

Auto-generated

method

stubSystem.out.println("FirstAcvity

>onDestory");super.onDestroy();}

@Overrideprotected

void

onPause()

{//

TODO

Auto-generated

method

stubSystem.out.println("FirstAcvity

>onPause");super.onPause();}

@Overrideprotected

void

onRestart()

{//

TODO

Auto-generated

method

stubSystem.out.println("FirstAcvity

>onRestart");super.onRestart();}

@Overrideprotected

void

onResume()

{//

TODO

Auto-generated

method

stubSystem.out.println("FirstAcvity

>onResume");super.onResume();}

@Overrideprotected

void

onStart()

{//

TODO

Auto-generated

method

stubSystem.out.println("FirstAcvity

>onStart");super.onStart();}

@Overrideprotected

void

onStop()

{//

TODO

Auto-generated

method

stubSystem.out.println("FirstAcvity

>onStop");super.onStop();}

class

ButtonListener

implements

OnClickListener

{

@Overridepublic

void

onClick(View

v)

{//

TODO

Auto-generated

method

stubIntent

intent

=

new

Intent();intent.setClass(FirstActivity.this,

SecondActivity.class);intent.putExtra("extra_data",

"Hello

I'm

FirstActivity!");FirstActivity.this.startActivity(intent);}

}

}

//mian.xml<?xml

version="1.0"

encoding="utf-8"?><LinearLayout

xmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

><TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/><Buttonandroid:id="@+id/myButton"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/first_button"/></LinearLayout>

//SecondActivity.java

package

mars.activity05;

import

android.app.Activity;import

android.content.Intent;import

android.os.Bundle;import

android.view.View;import

android.view.View.OnClickListener;import

android.widget.Button;import

android.widget.TextView;

public

class

SecondActivity

extends

Activity{

private

Button

secondButton;TextView

text

=

null;String

str

=

null;@Overrideprotected

void

onCreate(Bundle

savedInstanceState)

{System.out.println("SecondActivity>onCreate");//

TODO

Auto-generated

method

stubsuper.onCreate(savedInstanceState);setContentView(R.layout.second);secondButton

=

(Button)findViewById(R.id.secondButton);secondButton.setOnClickListener(new

ButtonListener());text

=

(TextView)

findViewById

(R.id.text);Intent

intent

=

getIntent();str

=

intent.getStringExtra("extra_data");text.setText(str);}

@Overrideprotected

void

onDestroy()

{//

TODO

Auto-generated

method

stubSystem.out.println("SecondActivity>onDestory");super.onDestroy();}

@Overrideprotected

void

onPause()

{//

TODO

Auto-generated

method

stubSystem.out.println("SecondActivity>onPause");super.onPause();}

@Overrideprotected

void

onRestart()

{//

TODO

Auto-generated

method

stubSystem.out.println("SecondActivity>onRestart");super.onRestart();}

@Overrideprotected

void

onResume()

{//

TODO

Auto-generated

method

stubSystem.out.println("SecondActivity>onResume");super.onResume();}

@Overrideprotected

void

onStart()

{//

TODO

Auto-generated

method

stubSystem.out.println("SecondActivity>onStart");super.onStart();}

@Overrideprotected

void

onStop()

{//

TODO

Auto-generated

method

stubSystem.out.println("SecondActivity>onStop");super.onStop();}

class

ButtonListener

implements

OnClickListener{

@Overridepublic

void

onClick(View

v)

{//

TODO

Auto-generated

method

stubIntent

intent

=

new

Intent();intent.setClass(SecondActivity.this,

FirstActivity.class);SecondActivity.this.startActivity(intent);}}}

//second.xml<?xml

version="1.0"

encoding="utf-8"?><LinearLayout

xmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView

android:id="@+id/text"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/second_button"

></TextView>

<Button

android:id="@+id/secondButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/second_button"

/></LinearLayout>

//AndroidManifest.xml

中注册

SecondActivity

<activity

android:name=".SecondActivity"

android:label="SecondActivity"

android:theme="@android:style/Theme.Dialog"/>

//string.xml

<?

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论