📜  具有演示应用程序的Android中的活动生命周期

📅  最后修改于: 2021-05-13 16:09:27             🧑  作者: Mango

在Android中,活动被称为应用程序中的一个屏幕。它与任何桌面应用程序的单窗口非常相似。一个Android应用包含一个或多个屏幕或活动。
每个活动都经过不同的阶段或生命周期,并由活动堆栈进行管理。因此,当一个新的活动开始时,前一个活动始终保持在其下方。活动分为四个阶段。

  1. 如果活动位于屏幕的前台,即堆栈的顶部,则称该活动为活动或正在运行。这通常是用户当前正在与之交互的活动。
  2. 如果某项活动失去了重点,并且非全尺寸或透明的活动已集中在您的活动之上。在这种情况下,另一个活动在多窗口模式下的位置较高,或者该活动本身在当前窗口模式下无法聚焦。这样的活动是完全活着的。
  3. 如果一个活动被另一个活动完全隐藏,则该活动将停止或隐藏。它仍然保留所有信息,并且由于其窗口是隐藏的,因此当其他地方需要内存时,它通常会被系统杀死。
  4. 系统可以通过要求活动结束或简单地终止其进程来从内存中销毁该活动。当再次将其显示给用户时,必须完全重新启动它并将其还原到以前的状态。

对于每个阶段,android为我们提供了一组7种方法,它们对生命周期中的每个阶段都有各自的意义。该图像显示了每当应用程序从一种状态切换到另一种状态时的迁移路径。

Android中的活动生命周期

每种方法的详细介绍如下:

1. onCreate()

在首次创建活动时调用它。这是完成所有静态工作的地方,例如创建视图,将数据绑定到列表等。此方法还提供了一个Bundle,其中包含以前的冻结状态(如果有的话)。

例子:

Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        // Bundle containing previous frozen state
        setContentView(R.layout.activity_main);
        
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
}


Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        // Bundle containing previous frozen state
        setContentView(R.layout.activity_main);
        
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onStart()
    {
        // It will show a message on the screen
        // then onStart is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_LONG).show();
    }
}


Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          
        // Bundle containing previous frozen state
        setContentView(R.layout.activity_main);
          
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onRestart() {
        // It will show a message on the screen
        // then onRestart is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onRestart Called", Toast.LENGTH_LONG).show();
    }
}


Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
import com.example.share.R;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
          
        // Bundle containing previous frozen state
        super.onCreate(savedInstanceState);
  
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        setContentView(R.layout.activity_main);
  
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onResume() {
        // It will show a message on the screen
        // then onResume is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onResume Called", Toast.LENGTH_LONG).show();
    }
}


Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
  
        // Bundle containing previous frozen state
        super.onCreate(savedInstanceState);
  
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        setContentView(R.layout.activity_main);
  
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onPause() {
        // It will show a message on the screen
        // then onPause is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onPause Called", Toast.LENGTH_LONG).show();
    }
}


Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
  
        // Bundle containing previous frozen state
        super.onCreate(savedInstanceState);
  
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        setContentView(R.layout.activity_main);
  
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onStop() {
        // It will show a message on the screen
        // then onStop is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onStop Called", Toast.LENGTH_LONG).show();
    }
}


Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
          
        // Bundle containing previous frozen state
        super.onCreate(savedInstanceState);
  
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        setContentView(R.layout.activity_main);
  
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onDestroy() {
        // It will show a message on the screen
        // then onDestroy is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onDestroy Called", Toast.LENGTH_LONG).show();
    }
}


Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onStart() {
        super.onStart();
        Toast toast = Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_LONG).show();
    }
  
    @Override
    protected void onRestart() {
        super.onRestart();
        Toast toast = Toast.makeText(getApplicationContext(), "onRestart Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onPause() {
        super.onPause();
        Toast toast = Toast.makeText(getApplicationContext(), "onPause Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onResume() {
        super.onResume();
        Toast toast = Toast.makeText(getApplicationContext(), "onResume Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onStop() {
        super.onStop();
        Toast toast = Toast.makeText(getApplicationContext(), "onStop Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onDestroy() {
        super.onDestroy();
        Toast toast = Toast.makeText(getApplicationContext(), "onDestroy Called", Toast.LENGTH_LONG).show();
    }
}


2. onStart()

当活动对用户可见时将调用它。如果从后台调用该活动,则紧随其后的是onResume()。首次启动活动时,也会在onCreate()之后调用它。

范例

Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        // Bundle containing previous frozen state
        setContentView(R.layout.activity_main);
        
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onStart()
    {
        // It will show a message on the screen
        // then onStart is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_LONG).show();
    }
}

3. onRestart()

在活动停止后和活动开始之前会调用它,因此,当任何活动从后台恢复到屏幕上时,总是会在其后跟随onStart()。

范例

Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          
        // Bundle containing previous frozen state
        setContentView(R.layout.activity_main);
          
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onRestart() {
        // It will show a message on the screen
        // then onRestart is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onRestart Called", Toast.LENGTH_LONG).show();
    }
}

4. onResume()

当活动开始与用户进行交互时,将调用它。此时,活动处于活动堆栈的顶部,用户与之交互。当活动进入后台或被用户关闭时,始终跟随onPause()。

范例

Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
import com.example.share.R;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
          
        // Bundle containing previous frozen state
        super.onCreate(savedInstanceState);
  
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        setContentView(R.layout.activity_main);
  
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onResume() {
        // It will show a message on the screen
        // then onResume is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onResume Called", Toast.LENGTH_LONG).show();
    }
}

5. onPause()

当活动进入后台但尚未被杀死时,将调用它。它与onResume()对应。当一个活动在另一个活动之前启动时,此回调将在顶部活动(当前在屏幕上)上调用。在活动活动的onPause()返回之前,不会创建活动活动下的活动,因此建议不要在此部分中进行繁重的处理。

范例

Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
  
        // Bundle containing previous frozen state
        super.onCreate(savedInstanceState);
  
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        setContentView(R.layout.activity_main);
  
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onPause() {
        // It will show a message on the screen
        // then onPause is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onPause Called", Toast.LENGTH_LONG).show();
    }
}

6. onStop()

当用户看不到活动时,将调用该方法。当活动从后台撤消时,其后是onRestart();当活动关闭或完成时,其后是onDestroy();当活动仅保留在后台时,则不执行任何操作。请注意,在内存不足的情况下,如果系统没有足够的内存来保持活动的进程在其onPause()方法被调用后仍无法运行,则永远不要调用此方法。

范例

Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
  
        // Bundle containing previous frozen state
        super.onCreate(savedInstanceState);
  
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        setContentView(R.layout.activity_main);
  
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onStop() {
        // It will show a message on the screen
        // then onStop is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onStop Called", Toast.LENGTH_LONG).show();
    }
}

7. onDestroy()

活动销毁之前收到的最终呼叫。这可能是由于活动即将完成(在调用finish()时),或者由于系统正在临时销毁该活动实例以节省空间而发生的。要区分这两种情况,请使用isFinishing()方法进行检查。

范例

Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
          
        // Bundle containing previous frozen state
        super.onCreate(savedInstanceState);
  
        // The content view pointing to the id of layout
        // in the file activity_main.xml
        setContentView(R.layout.activity_main);
  
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onDestroy() {
        // It will show a message on the screen
        // then onDestroy is invoked
        Toast toast = Toast.makeText(getApplicationContext(), "onDestroy Called", Toast.LENGTH_LONG).show();
    }
}

演示Android应用程序以演示Android中的活动生命周期

Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onStart() {
        super.onStart();
        Toast toast = Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_LONG).show();
    }
  
    @Override
    protected void onRestart() {
        super.onRestart();
        Toast toast = Toast.makeText(getApplicationContext(), "onRestart Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onPause() {
        super.onPause();
        Toast toast = Toast.makeText(getApplicationContext(), "onPause Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onResume() {
        super.onResume();
        Toast toast = Toast.makeText(getApplicationContext(), "onResume Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onStop() {
        super.onStop();
        Toast toast = Toast.makeText(getApplicationContext(), "onStop Called", Toast.LENGTH_LONG).show();
    }
  
    protected void onDestroy() {
        super.onDestroy();
        Toast toast = Toast.makeText(getApplicationContext(), "onDestroy Called", Toast.LENGTH_LONG).show();
    }
}

输出: