gpt4 book ai didi

android - android Activity 生命周期函数的基础知识

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:56 25 4
gpt4 key购买 nike

我正在测试这段显示 Activity 处于哪种状态的代码

public class Activity101Activity extends Activity {
String tag = "Lifecycle";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
setContentView(R.layout.activity_activity101);
Log.d(tag , "In the onCreate() event");
}
public void onStart()
{
super.onStart();
Log.d(tag , "In the onStart() event");
}

public void onRestart()
{
super.onRestart();
Log.d(tag , "In the onRestart() event");
}

public void onResume()
{
super.onResume();
Log.d(tag , "In the onResume() event");
}

public void onPause()
{
super.onPause();
Log.d(tag , "In the onPause() event");
}

public void onStop()
{
super.onStop();
Log.d(tag , "In the onStop() event" );
}

public void onDestroy()
{
super.onDestroy();
Log.d(tag , "In the onDestroy() event");
}
}

所以我看到 onDestroy() 仅在我们在屏幕上显示 Activity 时按下后退按钮时被调用,否则永远不会被调用。因此,如果我在 Activity 运行时按下主页按钮,它应该在后台运行。但是,如果我转到 Settings -> Apps -> Running,我在列表中看不到它。那么这是否意味着它是否在后台运行?

再一次,再一次,这段代码表明 onPause() 总是跟在 onStop() 之后,onStart() 总是跟在 onResume() 之后。那么为什么它们在Android环境中被定义为不同的功能而不是合并呢?

最佳答案

在这里我给你一个关于 Activity 生命周期的简单概念......

enter image description here

onCreate():

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().

onRestart():

Called after your activity has been stopped, prior to it being started again. Always followed by 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.

onResume():

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().

onPause():

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

停止():

Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

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.

所以..当Activity在没有用户交互的情况下出现在前台时调用onStart方法...和当用户开始交互时调用onResume方法...所以所有方法的功能都是不同的..

一旦你按下主页按钮或后退按钮,你的应用程序就会在后台运行,一旦你从任务管理器中将其杀死,它就不会在设置中显示任何内容......

关于android - android Activity 生命周期函数的基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18325654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com