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

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

问题描述

从Android开发中,我通过示例代码实现了一个简单的通知,但是在我的应用中,我不想重新创建意图并再次创建活动,我只想回到我的上一个活动(这是一个Mediaplayer 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开发中的示例代码,我评论了新的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中,我仍然看到,当我触摸通知以返回活动时,待处理的意图仍然调用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".

推荐答案

如果要从后台调用活动",请尝试以下操作:

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);

如果您在主屏幕上单击通知",则上一次显示的应用活动将在不重新启动的情况下进入前台.如果该活动被系统杀死,您将获得一个新的活动.

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天全站免登陆