android-Managing the Activity Lifecycle
来源:互联网 发布:虚幻4 编程 编辑:程序博客网 时间:2024/05/31 19:18
Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity.
There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.
》Implementing your activity lifecycle methods properly ensures your app behaves well in several ways, including that it:
- Does not crash if the user receives a phone call or switches to another app while using your app.
- Does not consume valuable system resources when the user is not actively using it.
- Does not lose the user's progress if they leave your app and return to it at a later time.
- Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.
<activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>
Note: When you create a new Android project with the Android SDK tools, the default project files include anActivity
class that's declared in the manifest with this filter.
SDK_INT
to prevent older systems from executing new APIs works in this way on Android 2.0 (API level 5) and higher only. Older versions will encounter a runtime exception.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // For the main activity, make sure the app icon in the action bar // does not behave as a button ActionBar actionBar = getActionBar(); actionBar.setHomeButtonEnabled(false); }
@Overridepublic void onDestroy() { super.onDestroy(); // Always call the superclass // Stop method tracing that the activity started during onCreate() android.os.Debug.stopMethodTracing();}》As long as the activity is still partially visible but currently not the activity in focus, it remains paused.
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.
For example, if your application uses the Camera
, the onPause()
method is a good place to release it.
@Overridepublic void onPause() { super.onPause(); // Always call the superclass method first // Release the Camera because we don't need it when paused // and other activities might need to use it. if (mCamera != null) { mCamera.release(); mCamera = null; }}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()
).The following example of onResume()
is the counterpart to the onPause()
example above, so it initializes the camera that's released when the activity pauses.
@Overridepublic void onResume() { super.onResume(); // Always call the superclass method first // Get the Camera instance as the activity achieves full user focus if (mCamera == null) { initializeCamera(); // Local method to handle camera init }}
- android-Managing the Activity Lifecycle
- Managing the Activity Lifecycle
- Managing the Activity Lifecycle
- Managing the Activity Lifecycle
- (2)Managing the Activity Lifecycle
- Managing the Activity Lifecycle笔记
- Android官方文档翻译 十六 4.Managing the Activity Lifecycle
- Activity生命周期管理(Managing the Activity Lifecycle)
- lesson 3 Managing the Activity Lifecycle
- AndroidTraining学习------Managing-the-Activity-Lifecycle
- AndroidTraining学习------Managing-the-Activity-Lifecycle-
- Android Develop Training——管理Activity的生命周期(Managing the Activity Lifecycle)
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Starting an Activity) (一)
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Pausing and Resuming an Activity) (二)
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Stopping and Restarting an Activity) (三)
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Recreating an Activity) (四)
- Managing the Activity Lifecycle--管理Activity生命周期
- (三)Managing the Activity Lifecycle管理activity的生命周期
- linux SHELL之结构化命令
- 堆排序heapsort
- shiro简单配置
- 关于js匿名函数的那些事
- c++标准库 及 命名空间std
- android-Managing the Activity Lifecycle
- vi、gcc、gdb基础
- 手把手教你用 Git
- pthread_create函数 pthread_self 函数
- service 总结之启动方式和生命周期
- 百度地图生成标注事件触发弹出SearchInfoWindow
- 日常总结
- 用户编程接口
- 电力企业计量生产运行系统总体解决方案