Dialog式的Activity(AndroidActivity生命周期)

概述

  和普通的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 }

 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 }

  并在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>

(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>

  当然,如果使用第二个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.
时间: 2024-11-10 07:58:16

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

Android探究2:Android 5.0下 Dialog&amp;AlertDialog 并不会影响Activity的生命周期

先给出结论:Dialog和AlertDialog并不会影响到Activity的生命周期,但会影响到Activity的优先级. 核心代码: onCreated中: Resources resources = this.getResources(); DisplayMetrics displayMetrics = resources.getDisplayMetrics(); final int widthPixels = displayMetrics.widthPixels / 2; final i

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

要学好活动(Activity),就必须要了解android中Activity的声明周期,灵活的使用生命周期,可以开发出更好的程序,在android中是使用任务来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈.栈的特性是后进先出,在默认的情况下,每当我们启动了一个新的活动,它会在返回栈中入栈,并处于栈顶的位置.而每当我们按下Back键或调用finish方法去销毁一个活动时,处于栈顶的活动会出栈,这时前一个入栈的活动就会重新处于栈顶的位置.系统总是会显示处于栈顶的活动给用户

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

Android开发艺术探索--第一章:Activity的生命周期和启动模式 怀着无比崇敬的心情翻开了这本书,路漫漫其修远兮,程序人生,为自己加油! 一.序 作为这本书的第一章,主席还是把Activity搬上来了,也确实,和Activity打交道的次数基本上是最多的,而且他的内容和知识点也是很多的,非常值得我们优先把他掌握,Activity中文翻译过来就是"活动"的意思,但是主席觉得这样翻译有些生硬,直接翻译成"界面"可能更好,的确,Activity主要也是用于UI效

第一章:Activity的生命周期和启动模式

Activity是Android中四大组件之首,所以需要重视,尤其是启动方式,在AndroidManifest.xml中的注册信息 (一)Activity的生命周期 1.1.1 正常情况下的生命周期 说明 (1)针对一个特定的Activity,第一次启动顺序:onCreate->onStart->onResume. (2)当用户打开新的Activity或者切换到桌面的时候,回调如下:onPause->onStop (3)返回原Activity时,回调如下:onRestart->on

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

上一篇文章讲了Activity的定义.状态和后退栈,现在讲讲Activity的重点和难点——Activity的生命周期. Activity生命周期的回调方法: //创建Activity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } //Activity快要变成可见的 @Override protected void onStart() { super.onStart(

(八)activity的生命周期

8.1 activity的7个生命周期方法 onCreate().onStart().onResume().onpause().onstop().ondestroy().onRestart()方法.onRestart()方法是当一个已经创建的Activity重新位于前台时,会调用此方法,其他的六个生命周期函数调用时机如下所示: 8.2 activity的生命周期 8.3  生命周期函数的应用场景 ondestroy():程序被销毁的时候调用,比较适合数据的持久化操作,保存数据. onStart(

Android activity 详解一:activity的生命周期

一.概述: Activity是android的四大组件之一,是用户接口程序,它会提供给用户一个交互式的接口功能.它是 android 应用程序的基本功能单元,其实Android中的Activity运行机制跟servlet有些相似之处,Android系统相当于servlet容器,Activity相当于一个servlet,我们的Activity处在这个容器中,一切创建实例.初始化.销毁实例等过程都是容器来调用的 ,activity 本身是没有界面的.所以activity类创建了一个窗口,开发人员可以

关于Activity及Activity的生命周期

1.Activity作用: 1)它是一个用户的接口,其是可见的,是一个应用程序的组件: 2)它是一个空间的组件,我们把所有的部件都放在里面: 2.创建Activity的要点: 1)一个Activity就是一个类,并且这个类要继承Activity 2)需要重写onCreat()方法: 3)每一个Activity都需要在AndroidMainfest.xml文件当中进行配置: [注意:<</span>intent-filter>.......</</span>inte

[Android基础]Activity的生命周期

今天面试被问及了一个问题:Activity A.Activity B,Activity A 被B覆盖的时候,Activity生命周期中哪几个方法被调用了?Activity A.Dialog B对话框覆盖了Activity A,Activity生命周期中哪些方法被调用了?答案等下揭晓: 我们经常使用Activity,也知道Activity的生命周期中有onCreate.onStart.onResume.onPause.onStop.onDestroy.onRestart这些方法,那么这些方法什么时