活动生命周期: 活动是Android操作系统的组成部分之一。简单地说,活动是用户与之交互的屏幕。 android中的每个活动都有生命周期,如创建、启动、恢复、暂停、停止或销毁。这些不同的状态称为 活动生命周期. 换句话说,我们可以说活动是预先编写的一个类 Java语言 编程。
目录
简短描述 活动生命周期 例子:
onCreate()– 首次创建活动时调用
onStart()– 仅在创建后调用,或在onStop()后通过restart方法调用。此处活动开始对用户可见
onResume()– 当活动对用户可见并且用户可以与其交互时调用
onPause()– 当活动内容不可见时调用,因为用户恢复了以前的活动
onStop()– 当活动对用户不可见时调用,因为发生了其他活动
onRestart()– 当用户出现在屏幕上或恢复已停止的活动时调用
onDestroy– 当活动不在后台时调用
活动具有不同的状态,或者称为 活动生命周期. 不需要重写所有生命周期方法,但理解它们非常重要。可以根据需求重写生命周期方法。
活动生命周期方法或状态列表:
创建的活动:onCreate(Bundle savedInstanceState):
当activity获取操作系统中的内存时,调用onCreate()方法。 要使用create state,我们需要重写onCreate(Bundle savedInstanceState)方法。 现在有一个问题是,这里的Bundle是什么,所以Bundle是一个数据存储库对象,可以存储任何类型的原始数据,并且这个对象将为null,直到一些数据没有保存在其中。
Learn More About onCreate(Bundle savedInstanceState)示例
活动已启动:onStart():
onStart()方法在创建后立即调用。在其他情况下,也可以通过调用restart方法来启动活动,即在活动停止之后。这意味着当用户 转换 在应用程序之间。例如,如果用户正在使用应用程序a,然后出现通知,用户单击通知并移动到应用程序B,在这种情况下,应用程序a将暂停。如果用户再次单击应用程序a的应用程序图标,则停止的应用程序a将再次启动。
Learn More About onStart()及其示例
活动已恢复:.onResume():
“活动已恢复”是指当用户实际可以看到它时,表示用户可以看到活动中显示的数据。在生命周期中,它总是在活动开始后调用,在大多数情况下,在活动暂停后调用(ON暂停).
活动已暂停:onPause():
当活动的内容对用户不可见时,称为暂停,在大多数情况下,当用户按Home键时,Android操作系统会调用onPause()方法 按钮 (居中 按钮 在设备上)进行隐藏。
如果用户按下后退导航键,活动也会在调用停止之前暂停 按钮. 如果通知或其他对话框覆盖活动(屏幕)的任何部分(顶部或底部),活动也会由于这些原因进入暂停状态。类似地,如果另一个屏幕或对话框是透明的,则用户可以看到该屏幕,但不能与其交互。例如,如果接到电话或通知,用户将有机会接听或忽略该电话。
Learn More About onPause() With Example
Activity Stopped: onStop():
Activity is called stopped when it’s not visible to user. Any activity gets stopped in case some other activity takes place of it. For example, if a user was on screen 1 and click on some button and moves to screen 2. In this case Activity displaying content for screen 1 will be stopped.
Every activity gets stopped before destroy in case of when user press back navigation button. So Activity will be in stopped state when hidden or replaced by other activities that have been launched or switched by user. In this case application will not present anything useful to the user directly as it’s going to stop.
Learn More About onStop() With Example
Activity Restarted: onRestart():
Activity is called in restart state after stop state. So activity’s onRestart() function gets called when user comes on screen or resume the activity which was stopped. In other words, when Operating System starts the activity for the first time onRestart() never gets called. It gets called only in case when activity is resumes after stopped state.
Activity Destroyed: onDestroy():
Any activity is known as in destroyed state when it’s not in background. There can different cases at what time activity get destroyed.
First is if user pressed the back navigation button then activity will be destroyed after completing the lifecycle of pause and stop.
In case if user press the home button and app moves to background. User is not using it no more and it’s being shown in recent apps list. So in this case if system required resources need to use somewhere else then OS can destroy the Activity.
活动销毁后,如果用户再次单击应用程序图标,则会重新创建活动,并再次遵循相同的生命周期。另一个用例是启动屏幕,如果有来自活动onCreate()的call to finish()方法,那么操作系统可以通过调用onPause()和onStop()直接调用onDestroy()。
在下面的示例中,我们使用了以下 JAVA 和Android主题:
使用的JAVA主题: 方法重写, 静态变量、包、继承、方法和类。
使用的Android主题: 我们使用了Log类,该类用于在Logcat中打印消息。日志的一个重要用途是在调试中。
首先,我们将创建一个新的Android项目,并将该活动命名为HomeActivity。在我们的案例中,我们将我们的应用程序项目命名为 活动生命周期 实例
我们将使用getSimpleName()方法初始化一个具有基础类名称的静态字符串变量。在我们的例子中,HOME\u ACTIVITY\u标记是存储类名HomeActivity的字符串变量的名称。
私有静态最终字符串HOME\u ACTIVITY\u TAG=HomeActivity。班getSimpleName();
现在,我们将创建一个新方法,该方法将在Logcat中打印消息。
私有void显示日志(字符串文本){ 日志d(HOME\u ACTIVITY\u标记,文本); }
现在,我们将重写Android中的所有活动生命周期方法,并使用我们为在Logcat中打印消息而创建的showLog()方法。
@覆盖 public void onCreate(Bundle savedInstanceState){ 超级的onCreate(savedInstanceState); 显示日志(“创建的活动”); } @覆盖 受保护的void onRestart(){ 超级的onRestart()//启动后调用重新启动 显示日志(“活动重新启动”); } @覆盖 受保护的void onStart(){ 超级的onStart()//很快就能看到 显示日志(“活动已启动”); } @覆盖 受保护的void onResume(){ 超级的onResume()//看得见的 显示日志(“活动已恢复”); } @覆盖 受保护的void onPause(){ 超级的onPause()//看不见的 显示日志(“活动暂停”); } @覆盖 受保护的void onStop(){ 超级的onStop(); 显示日志(“活动已停止”); } @覆盖 受保护的void ondestory(){ 超级的onDestroy(); showLog(“活动正在销毁”); }
完成HomeActivity的JAVA代码。java:
程序包com。abhiandroid。ActivityLifecycle示例; 导入android。操作系统。捆 导入android。支持设计小装置。浮动操作按钮; 导入android。支持设计小装置。Snackbar; 导入android。支持v7.app。AppCompatActivity; 导入android。支持v7.widget。工具栏; 导入android。util。日志 导入android。看法看法 导入android。看法菜单 导入android。看法菜单项; 公共类HomeActivity扩展了AppCompatActivity{ 私有静态最终字符串HOME\u ACTIVITY\u TAG=HomeActivity。班getSimpleName(); 私有void显示日志(字符串文本){ 日志d(HOME\u ACTIVITY\u标记,文本); } @覆盖 public void onCreate(Bundle savedInstanceState){ 超级的onCreate(savedInstanceState); 显示日志(“创建的活动”); } @覆盖 受保护的void onRestart(){ 超级的onRestart()//启动后调用重新启动 显示日志(“活动重新启动”); } @覆盖 受保护的void onStart(){ 超级的onStart()//很快就能看到 显示日志(“活动已启动”); } @覆盖 受保护的void onResume(){ 超级的onResume()//看得见的 显示日志(“活动已恢复”); } @覆盖 受保护的void onPause(){ 超级的onPause()//看不见的 显示日志(“活动暂停”); } @覆盖 受保护的void onStop(){ 超级的onStop(); 显示日志(“活动已停止”); } @覆盖 受保护的void ondestory(){ 超级的onDestroy(); showLog(“活动正在销毁”); } }
创建活动时,我们需要在AndroidManifest中注册该活动。xml 文件现在的问题是,为什么需要注册? 这实际上是因为manifest文件包含了Android操作系统首先读取的信息。 注册活动时,还可以在清单中定义其他信息,如启动器活动(当用户单击应用程序图标时应启动的活动)。
下面是AndroidManifest中的声明示例。xml 文件
<?xml版本=“1.0”编码=“utf-8”?> <清单xmlns:android=“http://schemas.android.com/apk/res/android" package=“com.abhiandroid.homeactivity”> <应用程序 android:allowBackup=“真” android:icon=“@mipmap/ic\u启动器” android:label=“@字符串/app\u名称” android:supportsRtl=“真” android:theme=“@style/AppTheme”> <活动 android:name=“.HomeActivity” android:label=“@字符串/app\u名称” android:theme=“@style/AppTheme.NoActionBar”> <目的过滤器> <action-android:name=“android.intent.action.MAIN”/> <类别android:name=“android.intent.category.LAUNCHER”/> </intent过滤器> </活动> </应用程序> </清单>
活动生命周期的输出:
当您运行上述程序时,您会注意到Emulator中将打开一个空白的白色屏幕。 您可能想知道默认的Hello world屏幕在哪里。实际上,我们通过重写onCreate()方法删除了它。 Below is the blank white screen that will pop up.
Now press the back button on the Emulator and exit the App:
So this clears:
Important Note: In the above example onRestart() won’t be called because there was no situation when we can resume the onStart() method again. In future example we will show you onRestart() in action as well.
Activity is the main component of Android Application, as every screen is an activity so to create any simple app first we have to start with Activities. Every lifecycle method is quite important to implement according to requirements, However onCreate(Bundle state) is always needed to implement to show or display some content on screen.
Hi Abhishek Saini Vaiya,
I hope that you are doing well. I am from Bangladesh. My name is Jahangir Alom Minto. I am learning Android App Development. I always follow your abhiandroid.com site. This is just a great resource and your Master Android Development ebook also a great material for android learners.
非常感谢您的伟大思想和对Android学习的免费贡献。
致以最诚挚的问候
贾汉吉尔·阿隆明托
这是非常好的信息。对于初学者来说,这些信息足以得到这份工作。