我应该如何在没有新意图的情况下从通知返回活动 [英] How should i do from notification back to activity without new intent

查看:27
本文介绍了我应该如何在没有新意图的情况下从通知返回活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 Android 开发,我通过示例代码实现了一个简单的通知,但在我的应用程序中,我不想新的意图并再次创建活动,我只想回到我的上一个活动(这是一个媒体播放器 UI).如果我使用示例代码,它将通过

from Android Development, i implement a simple notification by the sample code, but in my app, i don't want to new intent and create the activity again, i just want to back to my last activity(it's a mediaplayer UI). if i use the sample code, it will create a new activity by

Intent resultIntent = new Intent(this, ResultActivity.class);

我评论了关于新意图的相关代码并收到了通知,但不知道如何回到我的上一个活动......

i comment relative code about new intent and got a notification, but didn't have idea how to back to my last activity...

从长按主页键返回我的应用程序并触摸我的应用程序就可以了.我想要的就是这种行为.

back to my app from long press home key and touch my app is OK. what i want is just like this behavior.

bleow 是 Android Development 的示例代码,我对新的 Intent 部分进行了注释.

bleow is the sample code from Android Development, i comment the new intent portion.

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");
/*Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(
        0,
        PendingIntent.FLAG_UPDATE_CURRENT
    );
mBuilder.setContentIntent(resultPendingIntent);*/
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());

[更新]

我想我需要为活动的意图和属性设置标志.因此,对于我想返回的活动,我设置了

[UPDATE]

i thought i need to set flag for intent and properity for activity. therefore, for the activity i want to back to i set

android:launchMode="singleInstance"

并且因为,我不想要来自意图的新活动,我只想要我的最后一个活动,所以我添加

and because, i don't want a new activity from intent, i just want my last activity, so i add

Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

从文档中我了解到使用 Pendingintent.contentIntent 它必须包含 FLAG_ACTIVITY_NEW_TASK 标志,并且在使用此标志时,如果任务已经为您现在的活动运行开始,则不会开始新的活动;相反,当前任务将简单地以它上次进入的状态带到屏幕的前面.所以我还添加了

from documentation i got that using Pendingintent.contentIntent it must include the FLAG_ACTIVITY_NEW_TASK flag and When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. so i also add

Intent.FLAG_ACTIVITY_NEW_TASK

但是从 logCat 中,我仍然看到当我触摸返回活动的通知时,pendingintent 仍然调用 onCreate() 函数,而不仅仅是显示仍然活着"的最后一个活动.

but from logCat, i still saw that when i touch the notification for back to activity, the pendingintent still called onCreate() function rather than just show the last activity which still "alive".

推荐答案

如果你想从后台调用 Activity,试试这个:

If you want to call the Activity from background try this:

    Intent intent = new Intent(this, YourLauncherActivity.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            intent, 0);
    mBuilder.setContentIntent(pendingIntent);

如果您在主屏幕上单击通知,则您应用的最后显示的 Activity 将进入前台,而无需重新启动它.如果 Activity 被系统杀死,您将获得一个新的 Activity.

If you click the Notification while on Homescreen the last shown Activity of your App will be get to the foreground without starting it new. If the Activity was killed by the system you will get a new Activity.

这篇关于我应该如何在没有新意图的情况下从通知返回活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆