androidActivity生命周期
生活随笔
收集整理的這篇文章主要介紹了
androidActivity生命周期
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Activity生命周期
Activity是一個用來提供用戶交互界面的組件,它是四大組件之一,對于我們剛剛學習android的菜鳥來說是非常重要的,我們可以將一個屏幕理解為一個Activity,Activity通常是一個全屏的界面,每一個應用程序可以有一個或多個Activity,這里需要注意的是每一個Android應用都需要有一個入口Activity,也就是“MainActivity”,它作為程序的入口,就像Java應用中的main()方法一樣,當然與java中main()方法的名稱特定性不同,Android中入口Activity的名稱可以由程序員自行定義,只是需要在項目配置文件AndroidManifest.xml中將其配置為入口Activity即可。
它也同一個線程一樣是有生命周期的,下面看一下它的生命周期:
Activity共有以上7中狀態,下面看以代碼來理解各種狀態之間的轉化
? ?MainActivity.java
Act2.java
public class Act2 extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {setContentView(R.layout.act2);super.onCreate(savedInstanceState);System.out.println("2---onCreate");}@Overrideprotected void onStart() {System.out.println("2----onStart");super.onStart();/*Log.i("1", "info");Log.d("2", "debug");Log.v("3", "verbose");Log.e("4", "error");Log.w("5", "warning");*/}@Overrideprotected void onResume() {System.out.println("2---onResume");super.onResume();}@Overrideprotected void onPause() {System.out.println("2----onPause");super.onPause();}@Overrideprotected void onStop() {System.out.println("2---onStop");super.onStop();}@Overrideprotected void onRestart() {System.out.println("2---onRestart");super.onRestart();}@Overrideprotected void onDestroy() {System.out.println("2---onDestroy");super.onDestroy();} }另外兩個配置文件的代碼如下:
MainActivity.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/textView1"android:layout_marginLeft="72dp"android:layout_toRightOf="@+id/textView1"android:text="BUTTON" /> </RelativeLayout>act2.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World" /> </LinearLayout>運行結果如下:
?
運行時狀態如下所示:
轉載于:https://blog.51cto.com/maidoujava/1279893
總結
以上是生活随笔為你收集整理的androidActivity生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos akonadi mysql
- 下一篇: 从包中构建瓦片服务器