Android生命周期的学习

Java代码
package cn.com;

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 AndroidLifecycle extends Activity {

public void onCreate(Bundle savedInstanceState) {
System.out.println("First Activity =======onCreate()========");

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

Button btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(AndroidLifecycle.this, Second.class);
startActivity(intent);
}
});
}

// Called after onCreate — or after onRestart when the activity had been
// stopped, but is now again being displayed to the user. It will be
// followed by onResume
protected void onStart() {
System.out.println("First Activity =======onStart()========");
super.onStart();
}

// Called after onRestoreInstanceState, onRestart, or onPause, for your
// activity to start interacting with the user
protected void onResume() {
System.out.println("First Activity =======onResume()========");
super.onResume();
}

// Called as part of the activity lifecycle when an activity is going into
// the background, but has not (yet) been killed
protected void onPause() {
System.out.println("First Activity =======onPause()========");
super.onPause();
}

// Called when you are no longer visible to the user. You will next receive
// either onRestart, onDestroy, or nothing, depending on later user
// activity.
protected void onStop() {
System.out.println("First Activity =======onStop()========");
super.onStop();
}

// Perform any final cleanup before an activity is destroyed
protected void onDestroy() {
System.out.println("First Activity =======onDestroy()========");
super.onDestroy();
}

// Called after onStop when the current activity is being re-displayed to
// the user (the user has navigated back to it). It will be followed by
// onStart and then onResume
protected void onRestart() {
System.out.println("First Activity =======onRestart()========");
super.onRestart();
}

}

package cn.com;

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 AndroidLifecycle extends Activity {

public void onCreate(Bundle savedInstanceState) {
System.out.println("First Activity =======onCreate()========");

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

Button btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(AndroidLifecycle.this, Second.class);
startActivity(intent);
}
});
}

// Called after onCreate — or after onRestart when the activity had been
// stopped, but is now again being displayed to the user. It will be
// followed by onResume
protected void onStart() {
System.out.println("First Activity =======onStart()========");
super.onStart();
}

// Called after onRestoreInstanceState, onRestart, or onPause, for your
// activity to start interacting with the user
protected void onResume() {
System.out.println("First Activity =======onResume()========");
super.onResume();
}

// Called as part of the activity lifecycle when an activity is going into
// the background, but has not (yet) been killed
protected void onPause() {
System.out.println("First Activity =======onPause()========");
super.onPause();
}

// Called when you are no longer visible to the user. You will next receive
// either onRestart, onDestroy, or nothing, depending on later user
// activity.
protected void onStop() {
System.out.println("First Activity =======onStop()========");
super.onStop();
}

// Perform any final cleanup before an activity is destroyed
protected void onDestroy() {
System.out.println("First Activity =======onDestroy()========");
super.onDestroy();
}

// Called after onStop when the current activity is being re-displayed to
// the user (the user has navigated back to it). It will be followed by
// onStart and then onResume
protected void onRestart() {
System.out.println("First Activity =======onRestart()========");
super.onRestart();
}

}

Java代码
package cn.com;

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

public class Second extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
System.out.println("Second Activity =======onCreate()========");
}

// Called after onCreate — or after onRestart when the activity had been
// stopped, but is now again being displayed to the user. It will be
// followed by onResume
protected void onStart() {
System.out.println("Second Activity =======onStart()========");
super.onStart();
}

// Called after onRestoreInstanceState, onRestart, or onPause, for your
// activity to start interacting with the user
protected void onResume() {
System.out.println("Second Activity =======onResume()========");
super.onResume();
}

// Called as part of the activity lifecycle when an activity is going into
// the background, but has not (yet) been killed
protected void onPause() {
System.out.println("Second Activity =======onPause()========");
super.onPause();
}

// Called when you are no longer visible to the user. You will next receive
// either onRestart, onDestroy, or nothing, depending on later user
// activity.
protected void onStop() {
System.out.println("Second Activity =======onStop()========");
super.onStop();
}

// Perform any final cleanup before an activity is destroyed
protected void onDestroy() {
System.out.println("Second Activity =======onDestroy()========");
super.onDestroy();
}

// Called after onStop when the current activity is being re-displayed to
// the user (the user has navigated back to it). It will be followed by
// onStart and then onResume
protected void onRestart() {
System.out.println("Second Activity =======onRestart()========");
super.onRestart();
}

}

package cn.com;

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

public class Second extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
System.out.println("Second Activity =======onCreate()========");
}

// Called after onCreate — or after onRestart when the activity had been
// stopped, but is now again being displayed to the user. It will be
// followed by onResume
protected void onStart() {
System.out.println("Second Activity =======onStart()========");
super.onStart();
}

// Called after onRestoreInstanceState, onRestart, or onPause, for your
// activity to start interacting with the user
protected void onResume() {
System.out.println("Second Activity =======onResume()========");
super.onResume();
}

// Called as part of the activity lifecycle when an activity is going into
// the background, but has not (yet) been killed
protected void onPause() {
System.out.println("Second Activity =======onPause()========");
super.onPause();
}

// Called when you are no longer visible to the user. You will next receive
// either onRestart, onDestroy, or nothing, depending on later user
// activity.
protected void onStop() {
System.out.println("Second Activity =======onStop()========");
super.onStop();
}

// Perform any final cleanup before an activity is destroyed
protected void onDestroy() {
System.out.println("Second Activity =======onDestroy()========");
super.onDestroy();
}

// Called after onStop when the current activity is being re-displayed to
// the user (the user has navigated back to it). It will be followed by
// onStart and then onResume
protected void onRestart() {
System.out.println("Second Activity =======onRestart()========");
super.onRestart();
}

}


我们知道,在第一个Activity启动的时候,依次运行
onCreate()-->onStart()-->onResume(),当点击按钮启动第二个activity的时候,我们可以看到第一个activity的onPause()调用了,但是这个时候第一个activity还可以看到,没有被完全遮盖住,稍后第二个activitiy启动了,所以又依次调用了第二个activity的onCreate()-->onStart()-->onResume();这个时候第一个activity就完全不可见了,不可与user进行交互操作了,所以这个时候我们就看到第一个activity的onStop()方法就被调用了,但这个时候我们如果点击手机的back键,第2个activity就会调用onPause()方法,但是第一个activity又重新调回到最前面,所以现在调用的是onRestart()-->onStart()-->onResume();而这个时候第2个activity在第一个activity调用了onResume()后,又变的不可与user进行交互了,就调用了onStop()方法,这个时候我们要注意的是它又接着调用了onDestory()方法,因为activity是栈的原理,先进后出,第2个activity由于是后进(后显示出来),所以现在出去的话,就直接onDestory()掉了,如果这个时候我们在点击back退出的话,就会在依次调用第一个activity的
onPause()-->onStop()-->onDestory()方法;
以上说的估计看起来不是很明白,请见谅,我有个视频地址,是一个叫陈川的讲的,讲的还不错,贴上地址:http://u.youku.com/user_video/id_UMjYxMzI0MjQ4.html
里面的第7,8讲讲的很清楚,希望对学习的朋友有所帮助!!!

生命周期的在补充点:
Active/Runing一个新 Activity 启动入栈后,它在屏幕最前端,处于栈的最顶端,此时它处于可见并可和用户交互的激活状态.
Paused 当 Activity 被另一个透明或者 Dialog 样式的 Activity 覆盖时的状态.此时它依然与窗口管理器保持连接,系统继续维护其内部状态,所以它仍然可见,但它已经失去了焦点故不可与用户交互.
Stoped 当 Activity 被另外一个 Activity 覆盖、失去焦点并不可见时处于 Stoped状态.
Killed Activity 被系统杀死回收或者没有被启动时处于 Killed状态.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值