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