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 aBroadcastReceiver
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 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 ...
随机推荐
- 通过 IDE 向 Storm 集群远程提交 topology
转载: http://weyo.me/pages/techs/storm-topology-remote-submission/ http://www.javaworld.com/article/20 ...
- Altium Designer 从导入DXF文件,并转换成板框
大多数人都知道,PADS中导入DXF文件,然后转换成板框,是很方便的.AD也同样可以做到. PADS导入DXF见:http://www.cnblogs.com/craftor/archive/2012 ...
- 【Linux】鸟哥的Linux私房菜基础学习篇整理(十)
1. at [-mldv] TIME/at -c 工作号码:单一工作调度.参数:-m:当at的工作完成后,即使没有输出信息,以email通知用户该工作已完成:-l:相当于atq,列出目前系统上面的所有 ...
- java学习进制转换之查表法
10进制转16进制,以及10进制转2进制,还有10进制转8进制,这些转换如果按照常规思路的话,会灰常的麻烦. 我们来看一下 10进制转16进制: 假如这里有一个十进制数字:35,我们的需求就是把这个3 ...
- 【JS】壹零零壹
function f1() { } var f2 = function() { } var O = {} O.f1 = f1 O.f2 = f2 console.log(O)
- 网络流(最大流) POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8628 Accepted: 3636 ...
- 查看 AndroidManifest.xml文件
1.Manifest Explorer 装在Android手机中,用此apk看系统中已安装应用的AndroidManifest.xml文件: protected boolean configForPa ...
- G - Shuffle'm Up
题目大意: 是一个洗牌游戏,首先给出两堆牌,s1,s2,先从s1上面拿一张牌再从s2上面拿一张牌依次往下可以洗好牌,然后把洗好的牌再分成两堆继续洗,直到这堆牌的顺序与给的顺序相同可以停止,当然如果洗不 ...
- Hadoop 的子项目
Hadoop Common: 在0.20及以前的版本中,包含HDFS.MapReduce和其他项目公共内容,从0.21开始HDFS和MapReduce被分离为独立的子项目,其余内容为Hadoop Co ...
- Hadoop与HBase中遇到的问题
1. Hadoop中遇到的问题 曾经所遇到的问题因为没有记录,所以忘了 (1)NameNode没有启动成功, 是因为你对HDFS多次格式化,导致datanode中与namenode中的VERSION文 ...