zoukankan      html  css  js  c++  java
  • Activity生命周期

    生命周期流程图

     Activity的状态

      运行状态、暂停状态、停止状态、销毁状态

    说明:1》正常启动一个activity将先执行onCreate->onStart()->onResume然后进入运行状态

         2》当正在运行的activity被后来的activity覆盖(依然可见)时将执行onPause()进入暂停状态

       3》当activity不可见时将进入停止状态

         4》当activity结束或所在进程被销毁时将进入销毁状态

    回调方法

      在activity生命周期中,如下方法会被回调:

        onCreate   创建activity时被回调

        onStart     启动activity时被回调

        onReStart  当重新启动activity时被回调  (activity被停止后)

        onResume  恢复activity时被回调

        onPause     暂停activity时被回调

        onStop      停止activity时被回调

        onDestroy  销毁activity时被回调

     开发activity时可以选择性的覆盖一部分方法,其中onCreate方法是必须要覆盖的;其次还有onPause、onResume,比如在玩手机游戏过程中有来电,那么我们就可以覆盖此方法来保存游戏进度。当重新回到游戏时,再在onResume中恢复之。

     代码:

    View Code
     1 package com.example.activitylifecycle;
     2 
     3 import android.os.Bundle;
     4 import android.app.Activity;
     5 import android.util.Log;
     6 import android.view.Menu;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 
    10 public class ActivityLifeDemo extends Activity {
    11 
    12     private static final String TAG = "ActivityLifeDemo";
    13 
    14     @Override
    15     protected void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         Log.d(TAG,"onCreate");
    18         setContentView(R.layout.activity_activity_life_demo);
    19         findViewById(R.id.finish).setOnClickListener(new OnClickListener() {
    20 
    21             @Override
    22             public void onClick(View v) {
    23                 ActivityLifeDemo.this.finish();
    24             }
    25         });
    26     }
    27 
    28     @Override
    29     protected void onStart() {
    30         super.onStart();
    31         Log.d(TAG,"onStart");
    32     }
    33 
    34     @Override
    35     protected void onRestart() {
    36         Log.d(TAG,"onRestart");
    37         super.onRestart();
    38     }
    39 
    40     @Override
    41     protected void onResume() {
    42         Log.d(TAG,"onResume");
    43         super.onResume();
    44     }
    45 
    46     @Override
    47     protected void onPause() {
    48         Log.d(TAG,"onPause");
    49         super.onPause();
    50     }
    51 
    52     @Override
    53     protected void onStop() {
    54         Log.d(TAG,"onStop");
    55         super.onStop();
    56     }
    57 
    58     @Override
    59     protected void onDestroy() {
    60         Log.d(TAG,"onDestroy");
    61         super.onDestroy();
    62     }
    63 
    64 }

    启动应用:

      

    锁屏:

    开屏:

    调用finish,或back键

    按home键

     

  • 相关阅读:
    关于《浪潮之巅》
    C++知识点
    #ifndef/#define/#endif以及#if defined/#else/#endif使用详解
    typedef void(*Fun)(void);
    C#-StructLayoutAttribute(结构体布局)
    Web Services
    C# DataGridView
    VS2017编译boost库
    位与字节
    c++ map
  • 原文地址:https://www.cnblogs.com/byghui/p/3073012.html
Copyright © 2011-2022 走看看