玩了好久的Android突然发现让自己认认真真的说一下生命周期自己还有点不那么自信。今天就记录下来。
activity_my01.xml文件
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="跳转" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick2"
android:text="Dialog" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick3"
android:text="pop" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick4"
android:text="Dialog2" />
</LinearLayout>
MyActivity01.class:
public class MyActivity01 extends Activity {
private final String TAG = "MyActivity01";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my01);
Log.d(TAG, "onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
public void onClick(View view) {
Intent intent = new Intent(this, MyActivity02.class);
startActivity(intent);
}
public void onClick2(View view) {
// 弹出对话框
AlertDialog.Builder builder = new Builder(this);
builder.setMessage("确认退出吗?");
builder.setTitle("提示");
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
public void onClick3(View view) {
// 展示popWindow
View popView = LayoutInflater.from(this).inflate(R.layout.activity_pop,
null);
PopupWindow popupWindow = new PopupWindow(popView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(
android.R.color.transparent));
popupWindow.setOutsideTouchable(true);
int[] location = new int[2];
view.getLocationOnScreen(location);
// 该方法要发到最后,否者上面的set将无效
popupWindow.showAtLocation(view, Gravity.TOP, location[0] + 70,
location[1] + 100);
}
public void onClick4(View view) {
Intent intent = new Intent(this, DialogActivity.class);
startActivity(intent);
}
}
activity_dialog.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="i am dialog" />
</LinearLayout>
DialogActivity.class
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
}
另一类为随便写。
清单文件:
<activity
android:name=".demo1.MyActivity01"
android:label="@string/app_name"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".demo1.MyActivity02"
android:launchMode="singleTask" />
<activity
android:name=".demo1.DialogActivity"
android:launchMode="singleTask"
android:theme="@android:style/Theme.Dialog" />
</application>
其中DialogActivity的主题设置为对话框主题。
当启动MyActivity01时:
当跳转到MyActivity02,MyActivity01的生命周期:
其中MyActivity02的生命周期为onCreate,onStart,onResume,然后是MyActivity01的onStop。
然后点击back键回到MyActivity01:
当点击home键或者锁屏时:
当跳转到DialogActivity 时
由于DialogActivity并没有完全盖住MyActivity01,所以没走onStop();
当在Activity上弹出dialog或者popupwindow时生命周期没有任何反应。
以上七个方法中除了 onRestart()方法,其他都是两两相对的,从而又可以将活动分为三
种生存期。
1. 完整生存期
活动在 onCreate()方法和 onDestroy()方法之间所经历的,就是完整生存期。一般情
况下,一个活动会在 onCreate()方法中完成各种初始化操作,而在 onDestroy()方法中完
成释放内存的操作。
2. 可见生存期
活动在 onStart()方法和 onStop()方法之间所经历的,就是可见生存期。在可见生存
期内,活动对于用户总是可见的,即便有可能无法和用户进行交互。我们可以通过这两
个方法,合理地管理那些对用户可见的资源。比如在 onStart()方法中对资源进行加载,
而在onStop()方法中对资源进行释放, 从而保证处于停止状态的活动不会占用过多内存。
3. 前台生存期
活动在 onResume()方法和 onPause()方法之间所经历的,就是前台生存期。在前台
生存期内,活动总是处于运行状态的,此时的活动是可以和用户进行相互的,我们平时
看到和接触最多的也这个状态下的活动。