Activity的创建三部曲
- 新建类继承Activity或其子类
- 在AndroidManifest中声明
- 创建layout并在Activity的Oncreat中设置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test0508">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<activity android:name=".PopupWindowActivity"></activity>
<activity android:name=".CustonDialogActivity" />
<activity android:name=".ProgressActivity" />
<activity android:name=".Main2Activity" />
<activity android:name=".DialogActivity" />
<activity android:name=".ToastActivity" />
<activity android:name=".WebViewActivity" />
<activity android:name=".RecyclerView.PuRecycleViewActivity" />
<activity android:name=".RecyclerView.GridRecycleViewActivity" />
<activity android:name=".RecyclerView.HorRecycleViwActivity" />
<activity android:name=".RecyclerView.LinearRecycleViewActivity" />
<activity android:name=".RecyclerView.RecycleViewActivity" />
<activity android:name=".GridViewActivity" />
<activity android:name=".ImageViewActivity" />
<activity android:name=".CheckBoxActivity" />
<activity android:name=".RadioButtonActivity" />
<activity android:name=".EditTextActivity" />
<activity android:name=".MainActivity"
android:label="TestMain"
android:theme="@style/Theme.AppCompat.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestViewActivity" />
<activity android:name=".ButActivity" />
<activity android:name=".ListView.ListViewActivity" />
</application>
</manifest>
Activity的生命周期
刚点击Activity的时候,会先执行前三个方法
当从当前activity的页面返回时,会执行后三个方法
如果在当前页面来了个电话中断了,会进入调用pause方法
实际过程中打印日志
package com.example.test0508;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class LifeCycleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_life_cycle);
Log.d("LifeCycle","--onCreate---");
}
@Override
protected void onStart() {
super.onStart();
Log.d("LifeCycle","--onStart---");
}
@Override
protected void onResume() {
//每次进来数据都要刷新
super.onResume();
Log.d("LifeCycle","--onResume---");
}
@Override
protected void onPause() {
//应用退到后台,这个时候就暂停电影啊之类的
super.onPause();
Log.d("LifeCycle","--onPause---");
}
@Override
protected void onStop() {
super.onStop();
Log.d("LifeCycle","--onStop---");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("LifeCycle","--onRestart---");
}
@Override
protected void onDestroy() {
//页面摧毁之前要做什么事情
super.onDestroy();
Log.d("LifeCycle","--onDestroy---");
}
}
点击activity然后点击home键退到后台