生存或是毁灭,这是个问题
创建过滤器
?
运行
1.运行查看
?可知启动程序先掉用这三个方法
2.点击按钮"Start NormalActivity"
从MainAtivity跳转到NormalActivity
?在logcat中,Maintivity先调用onPause方法暂停,再调用onStop方法进入停止状态
?3.点击返回后
从停止状态到正常状态
4..点击按钮"START DIALOGACTIVITY"后
?进入暂停状态
?5.在安卓虚拟机中退出后
代码
结构
XML
activity_main.xml
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/bt_normal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Start NormalActivity"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/bt_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Start DialogActivity"/>
</LinearLayout>
</LinearLayout>
acticity_dialog.xml
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Start NormalActivity"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Start DialogActivity"/>
</LinearLayout>
</LinearLayout>
activity_normal.xml
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is a normal activity"/>
</LinearLayout>
</LinearLayout>
strings.xml
<resources>
<string name="app_name">LifeCycle</string>
<string name="title_activity_dialog">This is dialog activity</string>
</resources>
活动类
MainActivity
package com.star.lifecycle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Button btNor,btDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate");
btNor = findViewById(R.id.bt_normal);
btDialog = findViewById(R.id.bt_dialog);
btNor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,NormalActivity.class);
startActivity(intent);
}
});
btDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,DialogActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@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");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart");
}
}
NormalActivity
package com.star.lifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class NormalActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_normal);
}
}
DialogActivity
package com.star.lifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
}
|