关于android Activity生命周期的愚蠢问题
我有活动+服务课程。
当程序运行时,将显示活动并在活动的 onCreate
方法中启动服务。
活动将消失
- 当我单击虚拟机的“主页”按钮时,将出现“主页屏幕”,但此时
- ,我想知道该程序在哪种情况下 - 暂停/停止 - ???服务出了什么问题? (它仍在运行吗?)
- 如何使用该服务将活动置于前面?
谢谢...
I have activity+service classes.
When program runs activity will be shown and service will be started in onCreate
method of activity.
When I clicked on HOME button of virtual machine, HOME SCREEN will be appear but activity will have been gone
- at this point, I wonder that program in which situation -paused/stopped- ???
- what happened to the service? (is it still running?)
- how can I bring the activity to front using the service?
Thank you...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的情况下,如果您按主页按钮,活动就会暂停。通过按主页按钮再次启动它,它就会恢复。
对于服务:这取决于您的服务是如何启动的。
如果您使用 startService(intent) 启动服务,那么您的服务将运行直到:
a) 您通过调用 stopService 显式停止它。
b) android 杀死它
c) 你从服务内部调用 selfStop
如果你使用 bindService() 启动服务而不调用 startService() 那么服务将:
a) 运行直到 Activity 被销毁(不是暂停,所以这意味着如果您按主页按钮它将继续运行),这也意味着您需要调用 unbind()。
b)android杀死它
c)直到你从服务内部调用selfStop
将活动带到前台你需要调用一个意图并设置标志我相信它是从历史记录或类似的东西启动......你将不得不检查文档。
intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); <-我认为
或者这个 -> FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
Well in your case for the activity if you press the home button it is paused. and by launching it again with pressing the home button it just resumes it.
for the service: it depends how your service is started..
if you start the service with startService(intent) then your service is running until:
a) you explicitly stop it by calling stopService.
b) android kills it
c) you call selfStop from inside the service
if you start the service with bindService() without calling startService() then the service will:
a) run untill the activity is destroyed (not paused, so this means it will keep running if you press the home button) and this also means you need to call unbind().
b) android kills it
c) untill you call selfStop from inside the service
to bring the activity to foreground you need to call an intent and set the flag i believe it was launch from history or something like that.... you will have to check the docs.
intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); <- i think
or this one -> FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
请参阅下面的活动流程,希望对您有所帮助
See below the activity flow, hope it helps
您的
Activity
(和Service
)的状态可能会根据其他情况而变化。如果 Android 系统需要资源,它可能会破坏您的Activity
(也可能破坏您的Service
)来获取它们。您应该阅读:在
Activity
图中(也在Mark Bakker的回答中) 支付特别注意包含文本“其他应用程序需要内存”的框。The state of your
Activity
(andService
) could vary depending on what else is happening. If the Android system needed resources it may have destroyed yourActivity
(and possibly also yourService
) to obtain them. You should read up on:In the
Activity
diagram (also in Mark Bakker's answer) pay particular attention to the box containing the text 'Other applications need memory'.