Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32546
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 85
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-27 11:11
个人简介

everything is up to you

文章分类

全部博文(8)

文章存档

2014年(8)

分类: Android平台

2014-05-09 11:28:52

我的AndroidManifest.xml配置的版本是这样:(因为看其他帖子说不通的版本生命周期不一样
点击(此处)折叠或打开
  1. <uses-sdk
  2.         android:minSdkVersion="14"
  3.         android:targetSdkVersion="19" />
第一个Activity的代码:
点击(此处)折叠或打开
  1. package com.example.androidgov;

  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.DialogInterface;
  5. import android.content.DialogInterface.OnClickListener;
  6. import android.content.Intent;
  7. import android.content.res.Configuration;
  8. import android.os.Bundle;
  9. import android.view.View;

  10. public class TestActivityLifeF extends Activity {

  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         System.out.println("onCreate");
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.activity_test_activity_life_f);
  16.     }

  17.     public void startTest(View view) {
  18.         Intent intent = new Intent(TestActivityLifeF.this,
  19.                 StandardActivity.class);
  20.         startActivity(intent);
  21.     }

  22.     public void startDialog(View view) {
  23.         new AlertDialog.Builder(TestActivityLifeF.this).setTitle("Test")
  24.                 .setMessage("just test dialog").setPositiveButton("确定", new OnClickListener() {
  25.                     
  26.                     @Override
  27.                     public void onClick(DialogInterface dialog, int which) {
  28.                         
  29.                     }
  30.                 }).create().show();
  31.     }

  32.     @Override
  33.     protected void onStart() {
  34.         System.out.println("onStart");
  35.         super.onStart();
  36.     }

  37.     @Override
  38.     protected void onResume() {
  39.         System.out.println("onResume");
  40.         super.onResume();
  41.     }

  42.     @Override
  43.     protected void onPause() {
  44.         System.out.println("onPause");
  45.         super.onPause();
  46.     }

  47.     @Override
  48.     protected void onStop() {
  49.         System.out.println("onStop");
  50.         super.onStop();
  51.     }

  52.     @Override
  53.     protected void onDestroy() {
  54.         System.out.println("onDestroy");
  55.         super.onDestroy();
  56.     }

  57.     @Override
  58.     protected void onRestart() {
  59.         System.out.println("onRestart");
  60.         super.onRestart();
  61.     }

  62.     @Override
  63.     protected void onSaveInstanceState(Bundle outState) {
  64.         System.out.println("onSaveInstanceState");
  65.         super.onSaveInstanceState(outState);
  66.     }

  67.     @Override
  68.     protected void onRestoreInstanceState(Bundle savedInstanceState) {
  69.         System.out.println("onRestoreInstanceState");
  70.         super.onRestoreInstanceState(savedInstanceState);
  71.     }

  72.     @Override
  73.     public void onConfigurationChanged(Configuration newConfig) {
  74.         System.out.println("onConfigurationChanged");
  75.         super.onConfigurationChanged(newConfig);
  76.     }
  77. }
相应布局文件:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:id="@+id/container"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical"
  7.     tools:context="com.example.androidgov.TestActivityLifeF"
  8.     tools:ignore="MergeRootFrame" >

  9.     <Button
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:onClick="startTest"
  13.         android:text="启动Activity" />

  14.     <Button
  15.         android:layout_width="match_parent"
  16.         android:layout_height="wrap_content"
  17.         android:onClick="startDialog"
  18.         android:text="启动对话框" />

  19. </LinearLayout>
第二个Activity的代码:
点击(此处)折叠或打开
  1. package com.example.androidgov;

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

  4. public class StandardActivity extends Activity {

  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.activity_standard);
  9.     }

  10.     
  11. }
布局文件很简单,用默认生成的布局


启动第一个Activity:

切换到横屏:

看网上的帖子说横屏再切换成竖屏,生命周期的方法会调用两次,但我自己测试只调用了一次,如下(横屏切换回竖屏):

启动其他Activity:

从其他Activity中返回:

启动对话框和关闭对话框均不调用上面代码中列出的这些方法
最后退出Activity:


最后做个总结:
仅分析Activity中的10个方法:
onCreate()/onStart()/onResume()/onPause()/onStop()/onDestroy()/onRestart()/onSaveInstanceState()/onRestoreInstanceState()/onConfigurationChanged()
1:启动Activity:
onCreate()->onStart()->onResume()
2:竖屏切换横屏:
onPause()->onSaveInstanceState()->onStop()->onDestory()->onCreate()->onStart()->onRestoreInstanceState()->onResume()
3:横屏再切换回竖屏(和竖屏切换横屏一样)
onPause()->onSaveInstanceState()->onStop()->onDestory()->onCreate()->onStart()->onRestoreInstanceState()->onResume()
4:启动其他Activity:
onPause()->onSaveInstanceState()->onStop();
5:从其他Activity中返回:
onRestart()->onStart()->onResume()
6:启动对话框、关闭对话框,不调用上面列出的10个方法中任何一个
7:退出Activity:
onPause()->onStop()->onDestory()

阅读(270) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~