概述

  和普通的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. A Statistical View of Deep Learning (III): Memory and Kernels

    A Statistical View of Deep Learning (III): Memory and Kernels Memory, the ways in which we remember ...

  2. COJ 2106 road

    road 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:131072KB: 代码长度限制:102400B 试题描述   某国有N个城市,这N个城市由M条双向道路连接.现 ...

  3. 「Poetize4」玉蟾宫

    描述 Description 这片土地被分成N*M个格子,每个格子里写着'R'或者'F',R代表这块土地被赐予了rainbow,F代表这块土地被赐予了freda.现在freda要在这里卖萌...它要找 ...

  4. wcf系列学习5天速成——第三天 分布性事务的使用 有时间再验证下 不同库的操作

    原文地址:http://www.cnblogs.com/huangxincheng/archive/2011/11/06/2238273.html 今天是速成的第三天,再分享一下WCF中比较常用的一种 ...

  5. How to Implement the IContextMenu Interface

    http://msdn.microsoft.com/en-us/library/windows/desktop/hh127443(v=vs.85).aspx IContextMenu is the m ...

  6. 在Apache+php中使用json来通讯

    示例代码: <?php // 获取输入的内容 $request = http_get_request_body(); // 按json格式解析成一个 php对象 $json_obj = json ...

  7. 动态规划——树形dp

    动态规划作为一种求解最优方案的思想,和递归.二分.贪心等基础的思想一样,其实都融入到了很多数论.图论.数据结构等具体的算法当中,那么这篇文章,我们就讨论将图论中的树结构和动态规划的结合——树形dp. ...

  8. HDU 4751 Divide Groups 2013 ACM/ICPC Asia Regional Nanjing Online

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之 ...

  9. PHP面试题(二)

    前言 从网上找了一套号称是百度的php面试题目,这里记录一下 PHP的gc机制 php的垃圾回收机制注意以下几点即可: 引用计数refcount和is_ref,也就是php不会随意的malloc内存空 ...

  10. java 编辑报错 非法字符: \ufeff 解决方案

    用Notepad ++ 调成utf-8 格式 bom 或无bom根据情况 新建类 把代码一句句粘进去 ok