区分不同的 Activity 转换

发布于 2024-11-15 09:11:04 字数 342 浏览 1 评论 0原文

我有一个 Activity,可以从 ListActivity 中播放一些音频。用户还可以单击列表项并转到该数据的更详细视图。我希望音频在从一个活动转换到另一个活动期间继续播放,并在详细活动处于活动状态后继续播放,但如果我的活动因任何其他原因暂停或停止,我也需要暂停音频。

我的问题是,我目前在 ListActivity 的 onPause() 中暂停音频,因为我希望当用户离开我的 Activity 时暂停音频。例如,当他们按“Home”或“Back”时。但是,我不希望在详细视图活动开始时音频暂停。 onPause() 在这两种情况下都会被调用,那么我如何区分这两种情况呢?

I have an Activity that plays some audio from within a ListActivity. The user can also click on the list item and go to a more detailed view of that data. I want the audio to keep playing during that transition from one activity to another and keep playing once the detail activity is active BUT I also need to pause the audio if my activity is being paused or stopped for any other reason.

My issue is that I currently pause the audio in onPause() in the ListActivity because I want the audio to be paused when the user navigates away from my activity. E.g. when they press Home or Back. However, I don't want the audio to pause when my detailed view activity gets started. onPause() is called in both instances, so how can I distinguish between the two cases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2024-11-22 09:11:04

难道您不应该在 服务 中执行此操作吗?不是更容易吗?然后,您可以在一项或另一项活动暂停或退出时停止服务(取决于您想要哪一项)。您还可以检查它是否已启动,并在需要时相应地停止。

或者抱歉,如果我不明白你的问题。我看到你在这里有很多观点,所以也许我只是感到困惑。

--- 自您的第三条评论以来已编辑:

  1. A Intent to B:发送带有音乐名称的Intent
  2. B onCreate:从Intent获取音乐名称。将标志 x 设置为 true
  3. B onResume:如果从 A 开始播放音乐,如果从 B 恢复,则从上次已知位置恢复。
  4. B 后退按钮:覆盖并设置 xfalse。实际上,您将涵盖活动完成的所有点。
  5. B onPause:停止音乐if(x),将最后已知的音乐位置存储在内存中并停止服务。

在这里,我假设您希望音乐即使在返回 A 时也能继续播放(这就是您所说的,这就是问题),而不仅仅是 B。在我看来,尽早设置 x 很重要,因为如果有任何其他活动(电话、任何东西)出现时,Activity 将在 onPause 时立即停止播放音乐(用户期望如此)。根据 Android 指南,您知道只有当用户按下后退按钮或完成()您的活动时您才会返回到 A。您可以微调检查任务中活动的位置(现在不记得如何执行此操作)。

就我个人而言,我也不想在 A 上恢复播放(例如,B->呼叫->家->A,也将步骤 3 应用于 A),因为那里发生了事物逻辑流程的中断。

警告:我会确保没有其他方法。我会尝试看看您是否可以 1) 提前知道将要显示哪个活动。 2)获取任何音乐服务来连接您的任务(这可能吗?)。

无论如何,如果您找不到聪明的方法来做到这一点,请使用我的解决方案。这就是我的建议。祝你好运。

Shouldn't you be doing that in a service instead? Wouldn't it be easier? Then you can stop the service when one or another activity is pausing or exiting (depending on which one you want). You could also check if it's started, and stop accordingly whenever you want.

Or sorry if I didn't understand your question. I see you have a lot of points here on SO, so perhaps I'm just confused.

--- edited since your third comment:

  1. A Intent to B: send Intent with name of music.
  2. B onCreate: get name of music from Intent. Set flag x to true.
  3. B onResume: start playing music if from A or resumes from last known position if resuming from B.
  4. B back button: override and set x to false. Actually, you'd cover all points where your activity finishes.
  5. B onPause: stop the music if(x), store last known position of music in memory and stop service.

Here I assume that you want music to keep playing even when returning to A (that's what you said, that's the problem), not just up to B. Setting x is important early on, IMO, because if any other activity (phone call, anything) appears, activity will stop playing the music immediately (user expects that) on onPause. According to Android guidelines, you know that you're getting back to A only if user presses back button or if you finish() your activity. You can fine tuning checking the position of activities in your task (don't remember how to do that right now).

Personally, I also wouldn't want to resume playback on A (say, B->call->home->A, applying step 3 to A, too), because a disruption in the logical flow of things happens there.

CAVEAT: I would make sure that there is no other way. I would try to see if you can 1) know in advance which activity is about to be displayed. 2) get any music service to hook up to your task (is that even possible?).

Anyway, just use my solution if you can't find a clever way to do that. That would be my suggestion. Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文