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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

关于activity的生命周期1

發布時間:2024/3/13 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于activity的生命周期1 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
anctivity的生命周期比較復雜,但是很好的理解其生命周期,對于我們設計性能優良的應用程序有很大的幫助。
下面是官方文檔上的一幅比較經典的activity生命周期示意圖:


其中涉及到七個生命周期函數,下面摘自官方文檔的介紹: Method Description Killable? Next onCreate() onRestart() onStart() onResume() onPause() onStop() onDestroy()
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.

Always followed by?onStart().

No onStart()
???? Called after your activity has been stopped, prior to it being started again.

Always followed by?onStart()

No onStart()
Called when the activity is becoming visible to the user.

Followed by?onResume()?if the activity comes to the foreground, or?onStop()?if it becomes hidden.

No onResume()or?onStop()
???? Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.

Always followed by?onPause().

No onPause()
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.

Followed by either?onResume()?if the activity returns back to the front, or?onStop()?if it becomes invisible to the user.

Yes onResume()or
onStop()
Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.

Followed by either?onRestart()?if this activity is coming back to interact with the user, oronDestroy()?if this activity is going away.

Yes onRestart()or
onDestroy()
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called?finish()?on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the?isFinishing()?method. Yes nothing

文檔上已經很了然介紹了各函數的作用,什么時候被調用等,下面簡單的總結一下:
1.onCreate()
Activity首次被創建時被調用,用于設置activity的布局文件,綁定按鈕監聽器等一些靜態操作
2.onStart()
Activity對用戶可見時被調用
3.onResume()
Activity獲得用戶焦點,即用戶可以操作activity時被調用
4.onPause()
應用程序啟動了其他activity時被調用。一般用于當前activity中的數據
5.onStop()
activity不可見時被調用
6.onRestart()
已停止的activity重新啟動被調用
7.onDestroy()
調用activity的finish()方法或者android系統資源不足的時候被調用

我們通過簡單的實驗來說明一下:

下面的實驗是創建兩個activity,跳轉的過程中,通過activity生命周期函數來說明activity的生命過程。由于布局很簡單,下面只給出截圖:
activity_main.xml:


two.xml:


MainActivity:(下面通過重寫activity生命周期函數,來打印各個過程)

點擊(此處)折疊或打開

  • package com.example.android02;

  • import org.apache.commons.logging.Log;

  • 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;
  • import android.widget.TextView;

  • public class MainActivity extends Activity {

  • ????@Override
  • ????protected void onCreate(Bundle savedInstanceState) {
  • ????????super.onCreate(savedInstanceState);
  • ????????setContentView(R.layout.activity_main);
  • ????????//Log.i(TAG,"onCreat");
  • ????????TextView text = (TextView)findViewById(R.id.text1);
  • ????????text.setText(R.string.number1);
  • ????????Button button = (Button)findViewById(R.id.one);
  • ????????button.setText(R.string.change);
  • ????????button.setOnClickListener(new wang());
  • ????????
  • ????}
  • ????
  • ????class wang implements OnClickListener{
  • ????????
  • ????????public void onClick(View v){

  • ????????????Intent intent = new Intent();
  • ????????????intent.setClass(MainActivity.this,TwoActivity.class);
  • ????????????MainActivity.this.startActivity(intent);

  • ????????????}
  • ????????
  • ????????//public
  • ????????

  • ????}
  • ????
  • ????protected void onStart() {
  • ????????super.onStart();
  • ?????????System.out.println("first activity --》 onstart");
  • ????????// The activity is about to become visible.
  • ????}
  • ????@Override
  • ????protected void onResume() {
  • ????????super.onResume();
  • ????????System.out.println("first activity -->onresume");
  • ????????// The activity has become visible (it is now "resumed").
  • ????}
  • ????@Override
  • ????protected void onPause() {
  • ????????super.onPause();
  • ????????System.out.println("first activity -->onpause");
  • ????????// Another activity is taking focus (this activity is about to be "paused").
  • ????}
  • ????@Override
  • ????protected void onStop() {
  • ????????super.onStop();
  • ????????System.out.println("first activity -->onstop");
  • ????????// The activity is no longer visible (it is now "stopped")
  • ????}
  • ? ???protected void onRestart(){
    ? ? super.onRestart();
    ? ? System.out.println("first?activity -->restart");
    ? ?
    ? ? }
  • ????@Override
  • ????protected void onDestroy() {
  • ????????super.onDestroy();
  • ????????System.out.println("first activity -->ondestory");
  • ????????// The activity is about to be destroyed.
  • ????}
  • }

  • TwoActivity:

    點擊(此處)折疊或打開

  • package com.example.android02;

  • import android.app.Activity;
  • import android.os.Bundle;
  • import android.widget.Button;
  • import android.widget.TextView;

  • public class TwoActivity extends Activity{
  • ????
  • ????
  • ????@Override
  • ????protected void onCreate(Bundle savedInstanceState) {
  • ????????super.onCreate(savedInstanceState);
  • ????????setContentView(R.layout.two);
  • ????????TextView text = (TextView)findViewById(R.id.text2);
  • ????????text.setText(R.string.number2);
  • ????????@SuppressWarnings("unused")
  • ????????Button button = (Button)findViewById(R.string.button2);
  • ????}
  • ????
  • ????protected void onStart() {
  • ????????super.onStart();
  • ?????????System.out.println("second activity --》 onstart");
  • ????????// The activity is about to become visible.
  • ????}
  • ????@Override
  • ????protected void onResume() {
  • ????????super.onResume();
  • ????????System.out.println("second activity -->onresume");
  • ????????// The activity has become visible (it is now "resumed").
  • ????}
  • ????@Override
  • ????protected void onPause() {
  • ????????super.onPause();
  • ????????System.out.println("second activity -->onpause");
  • ????????// Another activity is taking focus (this activity is about to be "paused").
  • ????}
  • ????@Override
  • ????protected void onStop() {
  • ????????super.onStop();
  • ????????System.out.println("second activity -->onstop");
  • ????????// The activity is no longer visible (it is now "stopped")
  • ????}
  • ? ???protected void onRestart(){
    ? ? super.onRestart();
    ? ? System.out.println("second activity -->restart");
    ? ?
    ? ? }
  • ????@Override
  • ????protected void onDestroy() {
  • ????????super.onDestroy();
  • ????????System.out.println("second activity -->ondestory");
  • ????????// The activity is about to be destroyed.
  • ????}
  • }



  • 此時我們通過運行程序,通過Logcat可以看出打印出的各個生命周期階段:

    啟動后,日志輸出:(第一個activity經歷了oncreate,onstart,onresume,獲得焦點)

    點擊按鈕然后進入第二個activity:(第一個activity可以是可見,但是失去了焦點,第二個activity獲得了焦點)

    此時點擊back鍵:(此時第二個activity會失去焦點。而第一個activity之前被stop,此時會onrestart,重新獲得焦點。從上面的activity生命圖解可以很清楚的看出整個過程。這方面涉及到activity和Task相關知識,task以棧的形式存在,先啟動的activity會先入棧,最后啟動的activity會放在棧頂。上面第二個activity進棧的時候,此時完全遮蓋了第一個activity,所以導致了第一個activity被onstop,但是沒有被銷毀,這也是當第二個activity出棧,失去焦點后,第一個activity restart,而沒有oncreate的原因)


    來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/29876893/viewspace-1815730/,如需轉載,請注明出處,否則將追究法律責任。

    轉載于:http://blog.itpub.net/29876893/viewspace-1815730/

    總結

    以上是生活随笔為你收集整理的关于activity的生命周期1的全部內容,希望文章能夠幫你解決所遇到的問題。

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