安卓-生命周期

实验目的:

生成运行第一个Android程序,熟悉了解Android手机开发平台的基本构成和使用方法,熟悉Android应用程序项目的基本文件目录结构,了解开发环境提供的各种工具的基本用法。

在Activity中重载下9种事件函数,在调用不同函数时使用LogCat在Eclipse的控制台中输出调用日志。掌握Activity在启动、停止和销毁等不同阶段,9种重载函数的调用顺序,并掌握Android调试工具LogCat的使用方法

 1 package com.example.administrator.myapplication;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.util.Log;
 6 
 7 public class MainActivity extends AppCompatActivity {
 8 
 9     private static String TAG = "LIFECYCLE";
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15         Log.i(TAG, "onCreate");
16     }
17 
18     @Override
19     protected void onStart() {
20         super.onStart();
21         Log.i(TAG, "onStart");
22     }
23 
24     @Override
25     protected void onResume() {
26         super.onResume();
27         Log.i(TAG, "onResume");
28     }
29 
30     @Override
31     protected void onRestart() {
32         super.onRestart();
33         Log.i(TAG, "onRestart");
34     }
35 
36     @Override
37     protected void onPause() {
38         super.onPause();
39         Log.i(TAG, "onPause");
40     }
41 
42     @Override
43     protected void onStop() {
44         super.onStop();
45         Log.i(TAG, "onStop");
46     }
47 
48     @Override
49     protected void onDestroy() {
50         super.onDestroy();
51         Log.i(TAG, "onDestroy");
52     }
53 
54     @Override
55     protected void onSaveInstanceState(Bundle outState) {
56         super.onSaveInstanceState(outState);
57         Log.i(TAG, "onSaveInstanceState");
58     }
59 
60     @Override
61     protected void onRestoreInstanceState(Bundle savedInstanceState) {
62         super.onRestoreInstanceState(savedInstanceState);
63         Log.i(TAG, "onRestoreInstanceState");
64     }
65 
66 }
MainActivity.java
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     xmlns:app="http://schemas.android.com/apk/res-auto"
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent"
 8     tools:context="com.example.administrator.myapplication.MainActivity">
 9 
10     <TextView
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="Hello World!"
14         app:layout_constraintBottom_toBottomOf="parent"
15         app:layout_constraintLeft_toLeftOf="parent"
16         app:layout_constraintRight_toRightOf="parent"
17         app:layout_constraintTop_toTopOf="parent" />
18 
19 </android.support.constraint.ConstraintLayout>
activity_main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.administrator.myapplication" >
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:roundIcon="@mipmap/ic_launcher_round"
10         android:supportsRtl="true"
11         android:theme="@style/AppTheme" >
12         <activity android:name=".MainActivity" >
13             <intent-filter>
14                 <action android:name="android.intent.action.MAIN" />
15 
16                 <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
18         </activity>
19     </application>
20 
21 </manifest>
AndroidManifest.xml

猜你喜欢

转载自www.cnblogs.com/flyuz/p/10001773.html