android活动的生命周期
1.创建主活动
新建一个项目:ActivityLifeCycleTest,新建项目的过程中允许AndroidStudio帮我们自动创建活动与布局,并勾选Launcher Activity将创建的活动设置为主活动,创建的活动名与布局名都使用默认值。
我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:
2.创建两个子活动
两个子活动分别为NormalActivity和DialogActivity
右击com.example.activitylifecycletest包NewActivityEmpty Activity,新建NormalActivity,布局起名为normal_layout,同样的方法创建活动DialogActivity,布局起名为dialog_layout。
3.编辑normal_layout.xml文件
将里面的代码替换为如下代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a normal activity"
/>
</LinearLayout>
代码中使用一个TextView,用来显示一行文字
4.编辑dialog_layout.xml文件
将里面的代码替换为以下内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a dialog activity"
/>
</LinearLayout>
5.修改AndroidManifest.xml的标签配置
<activity android:name=".NormalActivity" ></activity>
<activity
android:name=".DialogActivity"
android:theme="@android:style/Theme.Dialog"></activity>
在DialogActivity的标签里加入android:theme="@android:style/Theme.Dialog ,使用android:theme属性,用于给当前的活动指定主题,这里让DialogActivity使用对话框式的主题。
6.修改activity_main.xml,重新定制主活动的布局
将里面的代码替换为如下内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/start_normal_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start NormalActivity"/>
<Button
android:id="@+id/start_dialog_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start DialogActivity"/>
</LinearLayout>
定义两个按钮,一个用于启动NormalActivity,一个用于启动DialogActivity
7.修改MainActivity中的代码
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button starNormalActivity = (Button) findViewById(R.id.start_normal_activity);
Button starDialogActivity = (Button) findViewById(R.id.start_dialog_activity);
starNormalActivity.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this,NormalActivity.class);
startActivity(intent);}
});
starDialogActivity.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
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 onDestory(){
super.onDestroy();
Log.d(TAG,"onDestory");
}
@Override
protected void onRestart(){
super.onRestart();
Log.d(TAG,"onRestart");
}
}
onCreate()方法中,为两个按钮注册点击事件,点击第一个按钮启动NormalActivity,点击第二个按钮启动DialogActivity,然后在7个回调函数中分别打印一句话,通过观察日志的方式直观理解活动的生命周期。