Dialog式的Activity(AndroidActivity生命周期)
概述
和普通的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 toonDestroy(). 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 toonStop(). 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 aBroadcastReceiverin 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 toonPause(). 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生命周期)的更多相关文章
- Android DevArt2:Android 5.0下 Dialog&AlertDialog 并不会影响Activity的生命周期
先给出结论:Dialog和AlertDialog并不会影响到Activity的生命周期,但会影响到Activity的优先级. 核心代码: onCreated中: Resources resources ...
- Android学习总结(一)——Activity的基本概念与Activity的生命周期
一.Activity的基本概念 Activity是Android的四大组件之一,它是一种可以包含用户界面的组件,主要用于和用户进行交互,比如打电话,照相,发送邮件,或者显示一个地图!Activity用 ...
- Android开发艺术探索笔记——第一章:Activity的生命周期和启动模式
Android开发艺术探索笔记--第一章:Activity的生命周期和启动模式 怀着无比崇敬的心情翻开了这本书,路漫漫其修远兮,程序人生,为自己加油! 一.序 作为这本书的第一章,主席还是把Activ ...
- Activity 之生命周期
Activity 之生命周期 本文内容: 1. Activity 介绍 2. Activity 的生命周期 2.1 生命周期图 2.2 常见情况下生命周期的回调 2.3 关于生命周期常见问题 2.4 ...
- android学习四(Activity的生命周期)
要学好活动(Activity).就必需要了解android中Activity的声明周期.灵活的使用生命周期.能够开发出更好的程序,在android中是使用任务来管理活动的,一个任务就是一组存放在栈里的 ...
- 从0系统学Android-2.4 Activity 的生命周期
本系列文章,参考<第一行代码>,作为个人笔记 更多内容:更多精品文章分类 本系列持续更新中.... 2.4 Activity 的生命周期 掌握 Activity 的生命周期对于开发者来说是 ...
- 浅谈Android中Activity的生命周期
引言 我想对于Android开发人员来说,Activity是再熟悉不过了,今天我们就来探讨下Activity的生命周期.熟悉的掌握Activity对于开发健壮的Android应用程序来说至关重要.下面 ...
- Android四大组件之——Activity的生命周期(图文详解)
转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai 联系方式:JohnTsai.Work@gmail.com [Andro ...
- Activity的生命周期,BACK键和HOME键生命周期
Activity的生命周期模型在Google提供的官方文档上有比较详细的一个图示 public class HelloActivity extends Activity { public static ...
随机推荐
- 在浏览器控制台调试php程序
jsp中用system.out.print如果是在eclipse中调试的话,eclipse会自动拦截系统输出流, 然后输出在控制台中,而http输出流则不受影响,php好像无此功能, PHP是一种服务 ...
- 图论(网络流):[CTSC2001]终极情报网
[CTSC2001]终极情报网 [题目描述] 在最后的诺曼底登陆战开始之前,盟军与德军的情报部门围绕着最终的登陆地点展开了一场规模空前的情报战. 这场情报战中,盟军的战术是利用那些潜伏在敌军内部的双重 ...
- 动态规划(斜率优化):BZOJ 3675 [Apio2014]序列分割
Description 小H最近迷上了一个分割序列的游戏.在这个游戏里,小H需要将一个长度为N的非负整数序列分割成k+l个非空的子序列.为了得到k+l个子序列, 小H将重复进行七次以下的步骤: 1.小 ...
- 【最短路】FOJ 2243 Daxia like uber
题目链接: http://acm.fzu.edu.cn/problem.php?pid=2243 题目大意: 给一张N个点M条边的有向图,从s出发,把在x1的人送到y1,在x2的人送到y2用的最短距离 ...
- Java中的数据类型及相互转换方法
本文主要讲解两个部分: 一.Java中的数据类型有哪些? 二.数字类型和字符串类型相互转换的方法? 一.Java中的数据类型有哪些: Java中的数据类型有:基本数据类型和引用数据类型: 基本数据类型 ...
- 004_Eclipse编写第一个Java_Web程序
1.MyEclipse的菜单栏--File--Web Project,新建一个web工程 Project name 填上自己的项目名称,例如HelloWorld. 需要选择Java EE版本以及Tar ...
- bzoj2243 sdoi2011 染色 paint
明明是裸树剖 竟然调了这么久好蛋疼 大概是自己比较水的原因吧 顺便+fastio来gangbang #include<iostream> #include<cstring> # ...
- Java菜鸟学习笔记--Exception篇(一):异常简介
什么是异常(Exception)? 简述: 在运行过程中,应用程序可能遭遇各种严重程度不同的问题.异常提供了一种在不弄乱程序的情况下检查错误的巧妙方式.它也提供了一种直接报告错误的机制. 不同类型异常 ...
- Android Scroller类的详细分析
尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/7321910 Scroller这个类理解起来有一定的困难,刚开始接触Scrol ...
- Android图片框架---Glide
Glide *** 使用* 一.添加依赖* compile 'com.github.bumptech.glide:glide:3.7.0'* compile 'com.android.support: ...