日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

androidActivity生命周期

發布時間:2023/12/9 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 androidActivity生命周期 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Activity生命周期

Activity是一個用來提供用戶交互界面的組件,它是四大組件之一,對于我們剛剛學習android的菜鳥來說是非常重要的,我們可以將一個屏幕理解為一個ActivityActivity通常是一個全屏的界面,每一個應用程序可以有一個或多個Activity,這里需要注意的是每一個Android應用都需要有一個入口Activity,也就是“MainActivity”,它作為程序的入口,就像Java應用中的main()方法一樣,當然與javamain()方法的名稱特定性不同,Android中入口Activity的名稱可以由程序員自行定義,只是需要在項目配置文件AndroidManifest.xml中將其配置為入口Activity即可。

它也同一個線程一樣是有生命周期的,下面看一下它的生命周期:

Activity共有以上7中狀態,下面看以代碼來理解各種狀態之間的轉化

? ?MainActivity.java

下面看一下主要的代碼: public class MainActivity extends Activity {private Button btn;/**Activity四種狀態* 1運行態* 2暫停態* 3停止態* 4終止態*/@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);System.out.println("1----onCreate");btn = (Button) findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Intent i = new Intent(MainActivity.this,Act2.class);startActivity(i);}});}@Overrideprotected void onStart() {System.out.println("1----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("1---onResume");super.onResume();}@Overrideprotected void onPause() {System.out.println("1----onPause");super.onPause();}@Overrideprotected void onStop() {System.out.println("1---onStop");super.onStop();}@Overrideprotected void onRestart() {System.out.println("1---onRestart");super.onRestart();}@Overrideprotected void onDestroy() {System.out.println("1---onDestroy");super.onDestroy();} }

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生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。