Android生命周期的学习
16lz
2021-01-23
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() {@Overridepublic 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 onResumeprotected void onStart() {System.out.println("First Activity =======onStart()========");super.onStart();}// Called after onRestoreInstanceState, onRestart, or onPause, for your// activity to start interacting with the userprotected 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 killedprotected 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 destroyedprotected 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 onResumeprotected void onRestart() {System.out.println("First Activity =======onRestart()========");super.onRestart();}}
package cn.com;import android.app.Activity;import android.os.Bundle;public class Second extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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 onResumeprotected void onStart() {System.out.println("Second Activity =======onStart()========");super.onStart();}// Called after onRestoreInstanceState, onRestart, or onPause, for your// activity to start interacting with the userprotected 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 killedprotected 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 destroyedprotected 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 onResumeprotected 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状态.
KilledActivity 被系统杀死回收或者没有被启动时处于Killed状态.
更多相关文章
- android电量状态获取
- Android下拉列表使用方法
- Android获得屏幕分辨率的两种方法
- Android中getChildAt()方法介绍
- android 网络状态监听
- Android中NDK的使用方法
- 史上版本最全android源码下载地址
- Android的window类的常用方法
- android透明状态栏