Android:何时使用onStart(),onStop()? [英] android: when to use onStart(), onStop()?

查看:341
本文介绍了Android:何时使用onStart(),onStop()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几篇描述onStart()onResume()之间差异的文章:当活动可见时调用onStart(),当活动准备好与用户进行交互时调用onResume().很好.

I've read several posts that describe the difference between onStart() and onResume(): onStart() is called when the activity becomes visible, onResume() is called when the activity is ready for interaction from the user. fine.

我一直只是将代码添加到onPause()onResume(),而从未为onStart()onStop()所困扰.

I've always just added code to onPause() and onResume(), and never bothered with onStart() and onStop().

谁能给出一些具体示例,说明您在onStart()onResume()中可能会做什么? onStop()onPause()也是一样,onStop()有何用处?我必须在这里错过一些基本的东西.

Can anyone give some concrete examples of what you might do in onStart(), vs. onResume()? Same goes for onStop() and onPause(), how is onStop() useful? I must be missing something fundamental here.

推荐答案

onStop()(几乎,请参阅有关对话框主题活动的常见评论). 例如,如果您在活动A中使用startActivity()来启动活动B.当您在活动B中按回时,您将返回到活动A,并且将调用onStart.

onStop() will (for example) be called when you leave the activity for some other activity (edit: almost. see commonswares comment about dialog themed activities). For example if you use startActivity() in activity A to start activity B. When you press back in activity B you will return to activity A and onStart will be called.

这与可能会在不调用onStop的情况下调用onPause的某些原因有所不同.例如,如果屏幕超时或您按下待机按钮onPause,但可能不会调用onStop(取决于可用内存和其他功能),则它称为较轻的暂停".即使在这种情况下,也可能最终会调用onStop,但不会立即调用.

This differs from some of the reasons onPause might be called without onStop being called. If for example the screen times out or you press the standy button onPause will be called, but probably not onStop (depending on memory available and whatnot), so it is a "lighter pause". onStop will be probably be called eventually even in this case, but not immediately.

好的,但是有什么用

通常没有特定用途,但可能有特定用途.由于即使在您启动其他活动之后,您的活动也将在堆栈上保留其内存状态,因此该堆栈将随着所启动活动的数量(堆栈的高度)而增加. 在某些应用程序中,这可能导致大量的内存使用.一段时间后,该框架将启动并终止堆栈上的某些活动,但这是比较直截了当的,并且可能意味着返回时将保留许多状态.

Often there is no specific use, but there might be. Since your activities will keep its memory state on the stack even after you start some other activity, that stack will increase with the number of activities started (height of the stack). This can lead to large memory usage in some applications. After a while the framework will kick in and kill some activities on the stack, but this is rather blunt and will probably mean a lot of states to be retained when returning.

所以onStart/onStop的一个示例用法是,如果您想在离开另一个活动时释放某种状态,而在返回活动时重新创建它. 我用它来设置listadapters为null,空的图像缓存和类似的东西(在非常特定的应用程序中).如果要释放listadapter中可见视图使用的内存,则可以在onstart上重新创建它,并让gc拾取视图.这将增加活动剩余内存状态继续存在的可能性.

So an example use for onStart/onStop is if you want to release some state when leaving an activity for another and recreate it when you get back. I have used it to set listadapters to null, empty image caches and similar (in very specific applications). If you want to free the memory used by visible views in a listadapter you can recreate it in onstart and let the views be picked up by the gc. This will increase the likelyhood that the rest of the memory state of the activity will live on.

在活动实例处于活动状态时,某些资源可以被认为足以保存,而只有在其位于堆栈的前端时,某些资源才可以保存.由您决定什么是应用程序中最好的,而create/start/resume的粒度可为您提供.

Some resources can be deemed good enough to save while the activity instance is alive and some only when it is on the front of the stack. It is up to you to decide what is best in your application and the granularity of create/start/resume gives you that.

这篇关于Android:何时使用onStart(),onStop()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆