Android学习历程-Activity生命周期
里面写了三个Activity:
package com.example.activity.life.cycle; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class FirstActivity extends Activity { private Button button = null; @Override protected void onCreate(Bundle savedInstanceState) { System.out.println("FirstActivity---> onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.first_activity); button = (Button)findViewById(R.id.firstButton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(FirstActivity.this, SecondActivity.class); FirstActivity.this.startActivity(intent); } }); } @Override protected void onDestroy() { System.out.println("FirstActivity---> onDestroy"); super.onDestroy(); } @Override protected void onPause() { System.out.println("FirstActivity---> onPause"); super.onPause(); } @Override protected void onRestart() { System.out.println("FirstActivity---> onRestart"); super.onRestart(); } @Override protected void onResume() { System.out.println("FirstActivity---> onResume"); super.onResume(); } @Override protected void onStart() { System.out.println("FirstActivity---> onStart"); super.onStart(); } @Override protected void onStop() { System.out.println("FirstActivity---> onStop"); super.onStop(); } }
package com.example.activity.life.cycle; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SecondActivity extends Activity { private Button button = null; @Override protected void onCreate(Bundle savedInstanceState) { System.out.println("SecondActivity:>>>>----- onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); button = (Button) findViewById(R.id.secondButton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(SecondActivity.this, ThirdActivity.class); SecondActivity.this.startActivity(intent); finish(); } }); } @Override protected void onDestroy() { System.out.println("SecondActivity:>>>>----- onDestroy"); super.onDestroy(); } @Override protected void onPause() { System.out.println("SecondActivity:>>>>----- onPause"); super.onPause(); } @Override protected void onRestart() { System.out.println("SecondActivity:>>>>----- onRestart"); super.onRestart(); } @Override protected void onResume() { System.out.println("SecondActivity:>>>>----- onResume"); super.onResume(); } @Override protected void onStart() { System.out.println("SecondActivity:>>>>----- onStart"); super.onStart(); } @Override protected void onStop() { System.out.println("SecondActivity:>>>>----- onStop"); super.onStop(); } }
package com.example.activity.life.cycle; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ThirdActivity extends Activity { private Button button = null; @Override protected void onCreate(Bundle savedInstanceState) { System.out.println("ThirdActivity:>>>>----- onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.third_acitvity); button =(Button)findViewById(R.id.thirdButton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Uri uri = Uri.parse("smsto://08000000123"); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", "The SMS text"); ThirdActivity.this.startActivity(intent); } }); } @Override protected void onDestroy() { System.out.println("ThirdActivity:>>>>----- onDestroy"); super.onDestroy(); } @Override protected void onPause() { System.out.println("ThirdActivity:>>>>----- onPause"); super.onPause(); } @Override protected void onRestart() { System.out.println("ThirdActivity:>>>>----- onRestart"); super.onRestart(); } @Override protected void onResume() { System.out.println("ThirdActivity:>>>>----- onResume"); super.onResume(); } @Override protected void onStart() { System.out.println("ThirdActivity:>>>>----- onStart"); super.onStart(); } @Override protected void onStop() { System.out.println("ThirdActivity:>>>>----- onStop"); super.onStop(); } }
三个布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/firstTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/first_activite" /> <Button android:id="@+id/firstButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="路转到第二个Activity"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/secondTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第二个activity" /> <Button android:id="@+id/secondButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="跳轉到ThirdActivity"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/thirdButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="跳轉到發送信息Activity"/> </LinearLayout>
string.xml文件:
<resources> <string name="app_name">ActivityLifeCycle</string> <string name="first_activite">First Activity</string> </resources>
Mainfest.xml文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activity.life.cycle" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.activity.life.cycle.FirstActivity" android:label="第一個Activity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.activity.life.cycle.SecondActivity" android:label="第二個Activity" > </activity> <activity android:name="com.example.activity.life.cycle.ThirdActivity" android:label="第三個Activity"> </activity> </application> </manifest>