概述

  和普通的Activity跳转稍微不同的是,当第1个Activity跳转到第二个Activity后,如果点击'back'按钮(即Android键盘的按钮,则不会调用调用第一个Activity的onStop方法,因为弹出对话框的时候,第1个Activity对用户仍然是Visible(可见的).

  如下,定义了两个继承Activity的java类:

 package com.example.activitydialog;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button btn = null; @Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("MainActivity onCreate");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btn = (Button)findViewById(R.id.btnMain);
//pop a dialog activity.
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DialogActivity.class);
startActivity(intent);
}
});
} @Override
protected void onDestroy() {
System.out.println("MainActivity onDestroy");
// TODO Auto-generated method stub
super.onDestroy();
} @Override
protected void onPause() {
System.out.println("MainActivity onPause");
// TODO Auto-generated method stub
super.onPause();
} @Override
protected void onRestart() {
System.out.println("MainActivity onRestart");
// TODO Auto-generated method stub
super.onRestart();
} @Override
protected void onResume() {
System.out.println("MainActivity onResume");
// TODO Auto-generated method stub
super.onResume();
} @Override
protected void onStart() {
System.out.println("MainActivity onStart");
// TODO Auto-generated method stub
super.onStart();
} @Override
protected void onStop() {
System.out.println("MainActivity onStop");
// TODO Auto-generated method stub
super.onStop();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

 package com.example.activitydialog;

 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 DialogActivity extends Activity { private Button btn = null; @Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("DialogActivity onCreate");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog); btn = (Button) findViewById(R.id.btnDialog);
//go to the previous activity when click on the button.
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent(DialogActivity.this, MainActivity.class);
startActivity(intent);
}
});
} @Override
protected void onDestroy() {
System.out.println("DialogActivity onDestroy");
// TODO Auto-generated method stub
super.onDestroy();
} @Override
protected void onPause() {
System.out.println("DialogActivity onPause");
// TODO Auto-generated method stub
super.onPause();
} @Override
protected void onRestart() {
System.out.println("DialogActivity onRestart");
// TODO Auto-generated method stub
super.onRestart();
} @Override
protected void onResume() {
System.out.println("DialogActivity onResume");
// TODO Auto-generated method stub
super.onResume();
} @Override
protected void onStart() {
System.out.println("DialogActivity onStart");
// TODO Auto-generated method stub
super.onStart();
} @Override
protected void onStop() {
System.out.println("DialogActivity onStop");
// TODO Auto-generated method stub
super.onStop();
} }

  并在layout中分别定义两个不同的布局xml文件:

(MainActivity对应的布局)

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/btnMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/second_activity" /> </RelativeLayout>

(DialogActivity对应的布局)

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/btnDialog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/second_activity"
/>
</LinearLayout>

  当然,如果使用第二个Activity的按钮返回(而不是通过键盘的'返回'的话,还是会调用第一个Activity的onStop方法).

  这也充分说明了,官网对'Activity生命周期的这3段话):

There are three key loops you may be interested in monitoring within your activity:  

  • The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().
  • The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.
  • The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

Dialog式的Activity(AndroidActivity生命周期)的更多相关文章

  1. Android DevArt2:Android 5.0下 Dialog&AlertDialog 并不会影响Activity的生命周期

    先给出结论:Dialog和AlertDialog并不会影响到Activity的生命周期,但会影响到Activity的优先级. 核心代码: onCreated中: Resources resources ...

  2. Android学习总结(一)——Activity的基本概念与Activity的生命周期

    一.Activity的基本概念 Activity是Android的四大组件之一,它是一种可以包含用户界面的组件,主要用于和用户进行交互,比如打电话,照相,发送邮件,或者显示一个地图!Activity用 ...

  3. Android开发艺术探索笔记——第一章:Activity的生命周期和启动模式

    Android开发艺术探索笔记--第一章:Activity的生命周期和启动模式 怀着无比崇敬的心情翻开了这本书,路漫漫其修远兮,程序人生,为自己加油! 一.序 作为这本书的第一章,主席还是把Activ ...

  4. Activity 之生命周期

    Activity 之生命周期 本文内容: 1. Activity 介绍 2. Activity 的生命周期 2.1 生命周期图 2.2 常见情况下生命周期的回调 2.3 关于生命周期常见问题 2.4 ...

  5. android学习四(Activity的生命周期)

    要学好活动(Activity).就必需要了解android中Activity的声明周期.灵活的使用生命周期.能够开发出更好的程序,在android中是使用任务来管理活动的,一个任务就是一组存放在栈里的 ...

  6. 从0系统学Android-2.4 Activity 的生命周期

    本系列文章,参考<第一行代码>,作为个人笔记 更多内容:更多精品文章分类 本系列持续更新中.... 2.4 Activity 的生命周期 掌握 Activity 的生命周期对于开发者来说是 ...

  7. 浅谈Android中Activity的生命周期

    引言 我想对于Android开发人员来说,Activity是再熟悉不过了,今天我们就来探讨下Activity的生命周期.熟悉的掌握Activity对于开发健壮的Android应用程序来说至关重要.下面 ...

  8. Android四大组件之——Activity的生命周期(图文详解)

        转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai       联系方式:JohnTsai.Work@gmail.com       [Andro ...

  9. Activity的生命周期,BACK键和HOME键生命周期

    Activity的生命周期模型在Google提供的官方文档上有比较详细的一个图示 public class HelloActivity extends Activity { public static ...

随机推荐

  1. js弹出新窗口居中

    方式1: <script language="javascript"> var newUrl = <%=newUrl % > //window.locati ...

  2. HttpClient4.3.6 实现https访问

    package httptest; import java.io.IOException; import java.nio.charset.Charset; import java.security. ...

  3. 2014年Facebook的开源成就

    2014是Facebook开源硕果丰硕的一年,其开源项目经理詹姆斯·皮尔斯(James Pearce)连续12天发布开源博客文章展示全年该社交网站在此领域取得的成就. 皮尔斯公布的成就包括以下内容: ...

  4. Unity3d项目合作 场景的合并和还原

    Unity3d项目合作  场景的合并和还原 特别声明:转载自Unity3D研究院 如何侵犯版权,请通知我删除! 摘要: 导出Unity场景的所有游戏对象信息,一种是XML一种是JSON.本篇文章我们把 ...

  5. 2015 CCC - 02 找不匹配

    照例传送门CNUOJ - 0385:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=355 题目分析:首先感谢”数据结构与算法“群群友 ...

  6. (转载)PHP strtotime函数详解

    (转载)http://www.jb51.net/article/21495.htm strtotime函数是一个很好的函数,灵活的运用它,会给你的工作带来不少方便.但PHP的手册中却对此函数的参数没作 ...

  7. HDOJ(HDU) 2503 a/b + c/d(最大公约数问题)

    Problem Description 给你2个分数,求他们的和,并要求和为最简形式. Input 输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四个正 ...

  8. spj 设计

    在数据库中使用SELECT INTO 语句从SP.J.P表中创建一个新表J_P_SPJ新表J_P_QTY中的属性列包括:工程号 (JNO).工程名(JNAME).零件号(PNO).零件名(PNAME) ...

  9. [Locked] One Edit Distance

    One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. 分析: ...

  10. [Locked] Walls and Gates

    Walls and Gates You are given a m x n 2D grid initialized with these three possible values. -1 - A w ...