Activity 的几个回调函数的用法

本文详细介绍了Android应用在不同生命周期阶段的操作,包括启动、暂停、停止和销毁的时机及对应的操作,重点阐述了如何在`onPause()`、`onStop()`和`onDestroy()`方法中进行资源清理和内存管理,确保应用高效运行并减少资源浪费。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You must implement the onCreate() method to perform basic application startup logic that should happen only once for the entire life of the activity. For example, your implementation of onCreate() should define the user interface and possibly instantiate some class-scope variables. such as declaring the user interface (defined in an XML layout file), defining member variables, and configuring some of the UI.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

your activity should perform most cleanup during onPause() and onStop(). However, if your activity includes background threads that you created during onCreate() or other long-running resources that could potentially leak memory if not properly closed, you should kill them during onDestroy()

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Note: The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method. In some cases, such as when your activity operates as a temporary decision maker to launch another activity, you might call finish()from within onCreate() to destroy the activity. In this case, the system immediately calls onDestroy()without calling any of the other lifecycle methods.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You should usually use the onPause() callback to:

  • Stop animations or other ongoing actions that could consume CPU.
  • Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
  • Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Generally, you should not use onPause() to store user changes (such as personal information entered into a form) to permanent storage. The only time you should persist user changes to permanent storage withinonPause() is when you're certain users expect the changes to be auto-saved (such as when drafting an email). However, you should avoid performing CPU-intensive work during onPause(), such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop()).

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Note: Because the system retains your Activity instance in system memory when it is stopped, it's possible that you don't need to implement the onStop() and onRestart() (or even onStart() methods at all. For most activities that are relatively simple, the activity will stop and restart just fine and you might only need to use onPause() to pause ongoing actions and disconnect from system resources.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Notice that no matter what scenario causes the activity to stop, the system always calls onPause() before callingonStop().

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you useonStop() to release resources that might leak memory.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

When your activity is stopped, the Activity object is kept resident in memory and is recalled when the activity resumes. The system also keeps track of the current state for each View in the layout, so if the user entered text into an EditText widget, that content is retained so you don't need to save and restore it.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

As your activity begins to stop, the system calls onSaveInstanceState() so your activity can save state information with a collection of key-value pairs. The default implementation of this method saves information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Instead of restoring the state during onCreate() you may choose to implementonRestoreInstanceState(), which the system calls after the onStart() method. The system callsonRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If called,  onSaveInstanceState() will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值