AndroidActivity生命周期

一、图解

 

 

二、概念分析

 

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

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

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

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

 

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

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

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

onPause() -> onStop() 方法 

 

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

 

当我们退出程序,或调用finish() 方法时,则依次调用 onPause() -> onStop() -> onDestroy() 方法

 

三、例子说明

 

//FirstActivity.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;

 

public class FirstActivity extends Activity {

/** Called when the activity is first created. */

private Button myButton;

 

@Override

public 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());

}

 

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

System.out.println("FirstAcvity --->onDestory");

super.onDestroy();

}

 

@Override

protected void onPause() {

// TODO Auto-generated method stub

System.out.println("FirstAcvity --->onPause");

super.onPause();

}

 

@Override

protected void onRestart() {

// TODO Auto-generated method stub

System.out.println("FirstAcvity --->onRestart");

super.onRestart();

}

 

@Override

protected void onResume() {

// TODO Auto-generated method stub

System.out.println("FirstAcvity --->onResume");

super.onResume();

}

 

@Override

protected void onStart() {

// TODO Auto-generated method stub

System.out.println("FirstAcvity --->onStart");

super.onStart();

}

 

@Override

protected void onStop() {

// TODO Auto-generated method stub

System.out.println("FirstAcvity --->onStop");

super.onStop();

}

 

class ButtonListener implements OnClickListener {

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent 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="http://schemas.android.com/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"

    />

<Button

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


@Override

protected void onCreate(Bundle savedInstanceState) {

System.out.println("SecondActivity--->onCreate");

// TODO Auto-generated method stub

super.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);

}

 

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

System.out.println("SecondActivity--->onDestory");

super.onDestroy();

}

 

@Override

protected void onPause() {

// TODO Auto-generated method stub

System.out.println("SecondActivity--->onPause");

super.onPause();

}

 

@Override

protected void onRestart() {

// TODO Auto-generated method stub

System.out.println("SecondActivity--->onRestart");

super.onRestart();

}

 

@Override

protected void onResume() {

// TODO Auto-generated method stub

System.out.println("SecondActivity--->onResume");

super.onResume();

}

 

@Override

protected void onStart() {

// TODO Auto-generated method stub

System.out.println("SecondActivity--->onStart");

super.onStart();

}

 

@Override

protected void onStop() {

// TODO Auto-generated method stub

System.out.println("SecondActivity--->onStop");

super.onStop();

}

 

class ButtonListener implements OnClickListener{

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent 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="http://schemas.android.com/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

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Hello World, FirstActivity!string>

    <string name="app_name">activity05string>

    <string name="first_button">启动第二个Activitystring>

    <string name="second_button">回到第一个Activitystring>

    

resources>

 


更多相关文章

  1. Android超精准计步器开发-Dylan计步
  2. android edittext 设置只允许输入整数,(设置输入类型)
  3. PreferenceActivity-Android的设置界面
  4. 【android】LayoutInflater.inflate方法的详解及xml根元素的布局
  5. 关于Android(安卓)Force Close 出现的原因 以及解决方法
  6. MVP+RxJava+Dagger打造的Android(安卓)Album
  7. android三种菜单介绍
  8. [Android] 问题记录 - Cupcake & Donut, Sdcard 正确使用方法
  9. android下播放器视频输出方法总结

随机推荐

  1. Android上oprofile使用说明
  2. 自定义Android(安卓)ListView控件:Expanda
  3. Android开发工程师三境界
  4. Android中资源的访问
  5. Android 常用控件讲解
  6. Eclipse上基于Android SDK的开发
  7. 用 Eclipse 开发 Android 应用程序
  8. Android 的视频编码 H263 MP4V H264
  9. Android 复习 Content Provider
  10. 【Android Developers Training】 53. 打