1. 程式人生 > >安卓-生命周期

安卓-生命周期

restart 應用程序 bsp nac n) ons 生成 create android調試

實驗目的:

生成運行第一個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

安卓-生命周期