Activity Lifecycle-Stopping and Restarting an Activity
来源:互联网 发布:小猪人脉 软件 编辑:程序博客网 时间:2024/06/07 04:35
There are a few of key scenarios in which your activity is stopped and restarted:
- The user opens the Recent Apps window and switches from your app to another app. The activity in your app that's currently in the foreground is stopped. If the user returns to your app from the Home screen launcher icon or the Recent Apps window, the activity restarts.
- The user performs an action in your app that starts a new activity. The current activity is stopped when the second activity is created. If the user then presses the Back button, the first activity is restarted.
- The user receives a phone call while using your app on his or her phone.
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.> When your activity receives a call to the
onStop()
method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy()
callback, so it's important you use onStop()
to release resources that might leak memory.@Overrideprotected void onStop() { super.onStop(); // Always call the superclass method first // Save the note's current draft, because the activity is stopping // and we want to be sure the current note progress isn't lost. ContentValues values = new ContentValues(); values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText()); values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle()); getContentResolver().update( mUri, // The URI for the note to update. values, // The map of column names and new values to apply to them. null, // No SELECT criteria are used. null // No WHERE columns are used. );}When your activity is stopped, the
Activity
object is kept resident in memory and is recalled when the activity resumes. You don’t need to re-initialize components that were created during any of the callback methods leading up to the Resumed state. 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. However, because your
onStop()
method should essentially clean up all your activity's resources, you'll need to re-instantiate them when the activity restarts. Yet, you also need to instantiate them when your activity is created for the first time (when there's no existing instance of the activity). @Overrideprotected void onStart() { super.onStart(); // Always call the superclass method first // The activity is either being restarted or started for the first time // so this is where we should make sure that GPS is enabled LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if (!gpsEnabled) { // Create a dialog here that requests the user to enable GPS, and use an intent // with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action // to take the user to the Settings screen to enable GPS when they click "OK" }}@Overrideprotected void onRestart() { super.onRestart(); // Always call the superclass method first // Activity being restarted from stopped state }>When the system destroys your activity, it calls the
onDestroy()
method for your Activity
. Because you should generally have released most of your resources with onStop()
, by the time you receive a call toonDestroy()
, there's not much that most apps need to do. This method is your last chance to clean out resources that could lead to a memory leak, so you should be sure that additional threads are destroyed and other long-running actions like method tracing are also stopped. 0 0
- Activity Lifecycle-Stopping and Restarting an Activity
- Stopping and Restarting an Activity
- Stopping and Restarting an Activity
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Stopping and Restarting an Activity) (三)
- android Training Stopping and Restarting an Activity
- 07Stopping and Restarting an Activity
- [Android Training视频系列]2.3 Stopping and Restarting an Activity
- Stopping and Restarting an Activity 停止和重新启动活动
- Android学习路线(十四)Activity生命周期——停止和重启(Stopping and Restarting)一个Activity
- Android学习路线(十四)Activity生命周期——停止和重启(Stopping and Restarting)一个Activity
- Android---Activity 生命周期(三)Stopping Activity && Restarting Activity
- Android官方Training阅读笔记 ---- Managing the Activity Lifecycle(Pausing and Resuming an Activity) (二)
- Activity生命周期管理之三——Stopping或者Restarting一个Activity
- Activity Lifecycle
- Activity Lifecycle
- Activity Lifecycle
- Activity Lifecycle & Fragment Lifecycle
- Activity生命周期(Activity Lifecycle)
- Objective-C Runtime 运行时之二:成员变量与属性
- Linux常用系统信息查看命令
- iOS cell重用机制导致数据重叠显示解决方法
- 指向结构体的指针
- 2015年12月24日 高程随堂测试 平安夜
- Activity Lifecycle-Stopping and Restarting an Activity
- Linux Mysql root 远程访问权限修改 默认字符集修改:
- VolleyPost请求,数组形式的参数有的手机提交不了
- 大数据/数据挖掘/推荐系统/机器学习相关资源
- 作为码农,我们为什么要写作
- [我与云表的故事]——相识三年,一见倾心(附分享一点干货)
- 如何做一个干货且装逼的产品经理演讲?
- java配置文件之xml和properties简单读取实例
- CoCos2d_android入门所需知道的一切