Dialog式的Activity(AndroidActivity生命周期)

本文探讨了Android中Activity的生命周期,特别是在使用对话框Activity时的行为差异。详细解释了从一个Activity跳转到另一个Activity时,不同操作对Activity生命周期方法的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

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

转载于:https://www.cnblogs.com/listened/p/4142646.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值