热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Dialog式的Activity(AndroidActivity生命周期)

概述和普通的Activity跳转稍微不同的是,当第1个Activity跳转到第二个Activity后,如果点击'back'按钮(即Android键盘的按钮,则不会调

概述

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

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

,,
 1 package com.example.activitydialog;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 
11 public class MainActivity extends Activity {
12 
13     private Button btn = null;
14     
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17 System.out.println("MainActivity onCreate");
18         // TODO Auto-generated method stub
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         
22         btn = (Button)findViewById(R.id.btnMain);
23         //pop a dialog activity.
24         btn.setOnClickListener(new OnClickListener() {
25             
26             @Override
27             public void onClick(View v) {
28                 Intent intent = new Intent(MainActivity.this, DialogActivity.class);
29                 startActivity(intent);
30             }
31         });
32     }
33 
34     @Override
35     protected void onDestroy() {
36 System.out.println("MainActivity onDestroy");        
37         // TODO Auto-generated method stub
38         super.onDestroy();
39     }
40 
41     @Override
42     protected void onPause() {
43         System.out.println("MainActivity onPause");        
44         // TODO Auto-generated method stub
45         super.onPause();
46     }
47 
48     @Override
49     protected void onRestart() {
50         System.out.println("MainActivity onRestart");
51         // TODO Auto-generated method stub
52         super.onRestart();
53     }
54 
55     @Override
56     protected void onResume() {
57         System.out.println("MainActivity onResume");
58         // TODO Auto-generated method stub
59         super.onResume();
60     }
61 
62     @Override
63     protected void onStart() {
64         System.out.println("MainActivity onStart");
65         // TODO Auto-generated method stub
66         super.onStart();
67     }
68 
69     @Override
70     protected void onStop() {
71         System.out.println("MainActivity onStop");
72         // TODO Auto-generated method stub
73         super.onStop();
74     }
75 
76 
77     @Override
78     public boolean onCreateOptionsMenu(Menu menu) {
79         // Inflate the menu; this adds items to the action bar if it is present.
80         getMenuInflater().inflate(R.menu.main, menu);
81         return true;
82     }
83 
84 }
View Code

,,
 1 package com.example.activitydialog;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 
10 public class DialogActivity extends Activity {
11 
12     private Button btn = null;
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         System.out.println("DialogActivity onCreate");
17         // TODO Auto-generated method stub
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_dialog);
20 
21         btn = (Button) findViewById(R.id.btnDialog);
22         //go to the previous activity when click on the button.
23         btn.setOnClickListener(new OnClickListener() {
24 
25             @Override
26             public void onClick(View v) {
27                 Intent intent = new Intent(DialogActivity.this, MainActivity.class);
28                 startActivity(intent);
29             }
30         });
31     }
32 
33     @Override
34     protected void onDestroy() {
35         System.out.println("DialogActivity onDestroy");
36         // TODO Auto-generated method stub
37         super.onDestroy();
38     }
39 
40     @Override
41     protected void onPause() {
42         System.out.println("DialogActivity onPause");
43         // TODO Auto-generated method stub
44         super.onPause();
45     }
46 
47     @Override
48     protected void onRestart() {
49         System.out.println("DialogActivity onRestart");
50         // TODO Auto-generated method stub
51         super.onRestart();
52     }
53 
54     @Override
55     protected void onResume() {
56         System.out.println("DialogActivity onResume");
57         // TODO Auto-generated method stub
58         super.onResume();
59     }
60 
61     @Override
62     protected void onStart() {
63         System.out.println("DialogActivity onStart");
64         // TODO Auto-generated method stub
65         super.onStart();
66     }
67 
68     @Override
69     protected void onStop() {
70         System.out.println("DialogActivity onStop");
71         // TODO Auto-generated method stub
72         super.onStop();
73     }
74 
75 }
View Code

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

(MainActivity对应的布局)

,,
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <Button
12         android:id="@+id/btnMain"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/second_activity" />
16 
17 RelativeLayout>
View Code

(DialogActivity对应的布局)

,,
 1 xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" 
 6     >
 7     <Button 
 8         android:id="@+id/btnDialog"
 9         android:layout_width="fill_parent"
10         android:layout_height="fill_parent"
11         android:text="@string/second_activity"
12         />
13 LinearLayout>
View Code

  当然,如果使用第二个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生命周期)


推荐阅读
  • 3295:[Cqoi2011]动态逆序对Description对于序列A,它的逆序对数定义为满足iAj的数对(i,j)的个数。给1到n的一个排列,按照某种顺序依次删除 ... [详细]
  • 猫猫分享,必须精品原文地址:http:blog.csdn.netu013357243articledetails44571163素材地址:http:download.csdn.n ... [详细]
  • 【自制小工具】代码生成器
    【自制小工具】代码生成器陆陆续续接触过好几款代码生成工具,发现确实好用,但都会有那么点不完善的地方,所以索性就自己做一个吧。界面非常简单,反正是自己用的,简单点用起来也方便上图:左 ... [详细]
  • kepserver中文手册,kepserver使用教程,kepserver设置
    下面介绍一下KepServer模拟器的使用,以下示例使用服务器随附的Simulator驱动程序来演示创建、配置和运行项目的过程。Simulator驱动程序是基于内存的驱动程序,能为 ... [详细]
  • 论文阅读及复现 | Improved Semantic Representations From TreeStructured Long ShortTerm Memory Networks
    两种形式的LSTM变体Child-SumTree-LSTMsN-aryTree-LSTMshttps:paperswithcode.compaperimproved-semanti ... [详细]
  • 看这里,教你如何快速将pdf文件翻译成中文
    因为网上下载的PDF资料,往往掺杂着一些英文,所以中英文翻译是一件很平常的事,毕竟不是每个人的英文都那么好,轻轻松松的就能够看完一篇英文的文件,那么,我们就要寻找翻译工具来帮助我们 ... [详细]
  • 以SOA服务为导向的信息系统构建是通过有计划地构建信息系统时,一种简单而有柔性的方法,就是组件化与服务导向架构。过去的信息系统,是在使用者需要新功能时才开发的,也就是响应不同时 ... [详细]
  • 简单动态字符串redis里面很多地方都用到了字符串,我们知道redis是一个键值对存储的非关系型数据库,那么所有的key都是用字符串存储的,还有字符串类型,这些都是用字符串存储的 ... [详细]
  • 例子如Table表有性别字段,1代表男2代表女、3代表中性、还有没填就代表未说明selectid,decode(sex,'1','男', ... [详细]
  • 接口测试的方式有很多,比如可以用工具(jmeter,postman)之类,也可以自己写代码进行接口测试,工具的使用相对来说都比较简单,重点是要搞清楚项目接口的协议是什么,然后有针对 ... [详细]
  • C#的Type对象的简单应用
    通过Type对象可以获取类中所有的公有成员直接贴代码:classMyClass{privatestringname;privateintid;publicstringcity;pu ... [详细]
  • 作业迁移
    背景:数据库服务器更换,1、数据库迁移(BACKUPRESTORE);2、数据库登录名用户迁移(注意孤立用户);3、作业迁移数据库迁移,备份数据库、拷贝备份文件到新服务器,还原数据 ... [详细]
  • Linux     系统安装
    Linux系统安装linux系统安装准备工作电脑、u盘、光盘、网络、硬盘主要使用光盘、网络虚拟化软件vmwarevi ... [详细]
  • 定义:定义两个数论函数\(f\)、\(g\)的Dirichlet卷积为:\[\left(f*g\right)\left(n\right)\sum_{d|n}f\left(d\rig ... [详细]
  • rbac 4表 常规设计
    rbac4表常规设计设计模型:1、管理员表(users)Schema::create('users',function(Blueprint$table){$tabl ... [详细]
author-avatar
mk艾草_180
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有