Android 避免 OnResume
我有 3 个基于 Activity 的应用程序,它的工作流程类似于 MainActivity
ListView
和 DetailView
。当onResume事件触发时,需要调用MainActivity。不去其他两项活动。
onResume 事件触发时有什么方法可以调用 MainActivity
吗?
谢谢
I have 3 Acivity based application, It's work flow like this MainActivity
ListView
and DetailView
. when onResume event trigger , need to call MainActivity. without going to other two activity.
Is there any way to call MainActivity
when onResume event trigger?
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 AndroidManifest.xml 文件中为 MainActivity 设置
android:clearTaskOnLaunch="true"
属性。请参阅此处了解原因和更多详细信息。我认为这是满足您需求的最便捷的方式。编辑:
我刚刚测试过,发现这只在您退出应用程序并从应用程序抽屉启动应用程序时才有效(不要长按主页并选择应用程序)。
如果您希望始终将根 Activity 置于最前面,无论您何时重新启动应用程序或从最近的屏幕启动。您可以为根 Activity(此处为 MainActivity)声明
"android:launchMode="singleTask"
。You can set the
android:clearTaskOnLaunch="true"
attribute for you MainActivity in the AndroidManifest.xml file. See here to find why and more details. I think this is the most convenient way to meet your demand.Edit:
I just tested and found this only works when you exit the app and launch the app from the app drawer(NOT long press on HOME and select the app).
If you want to always bring the root activity to the front, no matter when you re-launch the app or from the recent screen. You can declare
"android:launchMode="singleTask"
for the root activity, here, the MainActivity.我能想到的最好的解决方案是在所有其他活动的 onResume 中再次启动该活动:
但是,用户仍然可以点击后退按钮并返回到上一个活动。
The best solution I can think of is to start the activity again in the onResume of all your other activities:
The user will still be able to hit the back button and go back to the previous activity, however.
如果您想在用户关闭应用程序时退出列表/详细信息视图,请让它们在
onPause
中自行finish()
,当您的Activity< /代码> 已关闭。
这里唯一需要注意的是,调用
finish()
会将其移回 ActivityStack 中的一个 Activity,因此,如果您的 MainActivity 不是启动列表/详细信息视图的那个 Activity,则它不会返回到 MainActivity 。在这种情况下,您可以在 AndroidManifest.xml 中指定以防止将列表/详细信息活动放入历史记录中。
If you want to quit your List/Details views when the user closes your app, have them
finish()
themselves in theironPause
which is called when yourActivity
is closed.The only caveat here is that calling
finish()
will move it one Activity back in the ActivityStack so if your MainActivity isn't the one launching the List/Details views, it will not go back to the MainActivity. In this case, you could specify in the AndroidManifest.xmlto prevent the List/Details activities from ever being put into the history.