一种了解 Android 中按下 BACK 时将启动什么 Activity 的方法
我需要知道是否有任何方法可以知道在活动上按下后退按钮时将打开的活动是什么。我想我可以查看活动堆栈,但我需要一些关于如何执行此操作的指示。
提前致谢。
编辑:感谢所有答案,但我仍然想解释真正的问题。
我有一个应用程序,它有很多消耗大量电量的活动(传感器、GPS 和 WiFi),当我不使用该“任务”(即要做其他事情)时,我想保持安静。
我如何捕获后台堆栈中不再有我的应用程序的事件?
I need to know if there is any way to know when on an Activity what is the Activity that will be opened when the back button is pressed. I suppose i can take a look at the activity stack but i need some pointer on how to do it.
Thanks in advance.
EDIT: Thank for all the answers but i still want to explain the real problem.
I have an App that has a bunch of activities that consume a lot of power (sensor, gps and wifi), that i want to keep quiet when i'm not using that 'Task' (i.e going to do something else).
How can i trap the event of not having nothing more of my application in the back-stack?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 Intent Extra 从
ActivityX
发送到ActivityY
。 extra 可以是对标识活动的常量值的引用。ActivityX
:startActivity(new Intent(context, ActivityY.class).putExtra("fromActivity", Const.EXTRA_FROM_ACTIVITY_X));
ActivityYB
:其中
Const
是一个包含唯一的static Final int
变量的类,例如EXTRA_FROM_ACTIVITY_X
Send along an intent extra from
ActivityX
toActivityY
. The extra could be a reference to a constant value which identifies an activity.ActivityX
:startActivity(new Intent(context, ActivityY.class).putExtra("fromActivity", Const.EXTRA_FROM_ACTIVITY_X));
ActivityYB
:Where
Const
is a class holding uniquestatic final int
variables such asEXTRA_FROM_ACTIVITY_X
如果您可以猜测哪个活动,那么您可以在条件语句中使用instanceof方法
if you can guess which activity, then you can use the instanceof method in a conditional statement
查看此 Android 任务设计。
这应该也会有所帮助。
变量可用
taskAffinity
launchModeallowTaskReparentingclearTaskOnLaunchalwaysRetainTaskState
各种
finishOnTaskLaunch
API ,
:
see this task design for android.
this should help as well.
various APIs ,variables are available:
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch
我认为你应该更好地用更多细节描述你的问题,让我们帮助你找到最好的解决方案,而不是试图找到一种方法来实现你所选择的解决方案,这看起来很糟糕,因为它违反了原则和原则。 Android 操作系统的假设。
I think you should better describe your problem with more details to let us help you find the best solution, instead of trying to find a way to implement the solution you have chosen and which looks bad because it violates principles & assumptions of Android OS.