概述java
和普通的Activity跳轉稍微不一樣的是,當第1個Activity跳轉到第二個Activity後,若是點擊'back'按鈕(即Android鍵盤的按鈕,則不會調用調用第一個Activity的onStop方法,由於彈出對話框的時候,第1個Activity對用戶仍然是Visible(可見的).android
以下,定義了兩個繼承Activity的java類:app
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 }
和ide
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文件:oop
(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>
和this
(DialogActivity對應的佈局)spa
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方法).rest
這也充分說明了,官網對'Activity生命週期的這3段話):code
There are three key loops you may be interested in monitoring within your activity:
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().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.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.