通知启动多个 Activity 实例

发布于 2025-01-04 01:25:15 字数 327 浏览 4 评论 0原文

我正在开发一个媒体播放器应用程序,用于播放 SD 卡中的音频文件。通过使用该应用程序,我可以查看音频文件,可以播放所选的歌曲。 当播放歌曲时按下后退按钮时,会创建通知。问题就在这里,当我启动 MainActivity (通过使用 PendingIntent)时,应用程序正在启动 MainActivity 的新实例,而不是启动以前的 MainActivity 实例。

我在清单中将 MainActivity launchMode 设置为“Singletop”。仅供参考,该应用程序只有一个活动。

我也尝试添加通知标志,但应用程序仍然启动另一个实例。

出了什么问题,我无法得到。请帮帮我。

I am developing a media player app for playing the audio files in the SD card. By using the application, i can able to view the audio files, i am able to play the selected song.
when the back button is pressed while playing the song, notification is created. The problem comes here, when i am launching the MainActivity (by using the PendingIntent), the application is launching new instance of the MainActivity rather than launching the previous MainActivity instance.

I made the MainActivity launchMode to "Singletop" in the manifest.FYI, the application is having only one activity.

I tried to add the notification flags also, but still the application is launching another instance.

what went wrong, i am unable to get. Please help me out.

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

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

发布评论

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

评论(2

淡笑忘祈一世凡恋 2025-01-11 01:25:15
Intent intent=new Intent(this,Element.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

这对我有用:)

Intent intent=new Intent(this,Element.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

This worked for me :)

别再吹冷风 2025-01-11 01:25:15

老问题,但要集成 kishu27 解决方案(谢谢!),该解决方案有效,但唯一的缺点是将应用程序内的用户导航重置为清除堆栈的目的地意图(这可能是也可能不是您想要的),另一个解决方案是创建一个专门的活动,唯一的任务是成为意图的目的地:(

public class NotificationLanding extends Activity {

    @Override
    public void onCreate(Bundle mainBundle) {
        super.onCreate(mainBundle);

        View view = new View(this);
        view.setBackgroundColor(Color.TRANSPARENT);
        setContentView(view);

        finish();
    }
}

也不要忘记在清单中声明该活动!)

<activity android:name=".NotificationLanding"/>

并使用它来“集中”您的应用程序,而不影响您的导航用户。也许还有其他方法可以达到同样的效果,欢迎评论。

我建议还将 kishu27 Intent 标志集成到您的 Intent 中(我做到了),尽管这并不是绝对必要的。

希望有帮助

Old question, but to integrate kishu27 solution (thank you!), which works but has the only drawback of resetting the user navigation within the app to the destination intent clearing the stack (which may or may not be what you want), another solution is to create a specialised activity with the only task of be the destination of the Intent:

public class NotificationLanding extends Activity {

    @Override
    public void onCreate(Bundle mainBundle) {
        super.onCreate(mainBundle);

        View view = new View(this);
        view.setBackgroundColor(Color.TRANSPARENT);
        setContentView(view);

        finish();
    }
}

(also don't forget to declare the activity it in the manifest!)

<activity android:name=".NotificationLanding"/>

and use it to just "focus" your application without impacting the navigation of your user. Perhaps there are other means to get the same effect, comments are welcome.

I would advise to also integrate kishu27 Intent flags in your Intent (I did), although it's not strictly necessary.

hope it will help

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