意图用的onCreate()为singleTask活动老多 [英] Intent with old extra in onCreate() for singleTask Activity

查看:203
本文介绍了意图用的onCreate()为singleTask活动老多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我创建的项目有2个活动:FirstActivity与Android:launchMode =singleTask标志和SecondActivity。

For example I created project with 2 activities: FirstActivity with android:launchMode="singleTask" flag and SecondActivity.

在第一个用户开始FirstActivity这SecondActivity后,将开始按钮点击。从SecondActivity我们创建状态栏通知。

At first user starts FirstActivity and after this SecondActivity will start on button click. From SecondActivity we create status bar notification.

Intent intent = new Intent(SecondActivity.this, FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(FirstActivity.ARG, true);

NotificationCompat.Builder builder = new      NotificationCompat.Builder(SecondActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher)
   .setAutoCancel(true)
   .setContentTitle("title")
   .setContentText("message");

PendingIntent notifyIntent = PendingIntent.getActivity(SecondActivity.this, 0,     intent,PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(notifyIntent);

NotificationManager notificationManager = (NotificationManager)   getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1000, builder.build());

当我点击该通知,现在我的应用程序正在运行,FirstActivity#onNewIntent方法将被执行,并意图包含ARG额外=真。这是确定的情况。

When I clicked on this notification and my application is running now, FirstActivity#onNewIntent method will be executed and intent contains ARG extra = true. It's Ok situation.

当我点击该通知,我的应用程序没有开始运行,FirstActivity#onNewIntent方法将不会被执行,而是这个FirstActivity#的onCreate()将被执行,我可以让我的阿根廷国旗样      getIntent()。getBooleanExtra(阿根廷,FALSE) 此意图包含ARG额外= TRUE。这是正确的情况了。

When I clicked on this notification and my application is NOT running now, FirstActivity#onNewIntent method will NOT be executed, but instead of this FirstActivity#onCreate() will be executed and I can get my ARG flag like getIntent().getBooleanExtra(ARG, false) This intent contains ARG extra = true. It's correct situation too.

但是,当我经过情况2 pressing后退按钮尝试退出我的应用程序,并再次运行应用程序,我FirstActivity#的onCreate()接收的意图与ARG = TRUE(旧额外的价值,我处理)。之后重新打开应用程序为什么我的标志为真?

But when I try exit from my app after Situation 2 pressing back button and run app again, I received in FirstActivity#onCreate() intent with ARG = true (old extra value which I handled). Why my flag is "true" after re-open app?

推荐答案

我遇到了这个问题,同时寻找解决类似的问题。尽管这个问题是从一年之前,我会记下我是如何在情况下,它可以帮助别人,因为我花了很多很多时间来解决我的问题解决了。 我也有android的声明的活动:launchMode为singleTask。通知是从服务发送,这个通知具有一个Intent打开MyActivity一个PendingIntent。

I came across this question while looking for a solution to a similar problem. Even though this question is from an year ago, I’ll write down how I solved it in case it can help somebody else because it took me many many hours to solve my problem. I also have the activity declared with the android:launchMode as "singleTask". A notification is sent from a service and this notification has a PendingIntent with an Intent to open MyActivity.

我的解决办法是设置标志FLAG_ACTIVITY_NEW_TASK(的http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK)在意图。 然后创建挂起的意图id为0和PendingIntent.FLAG_CANCEL_CURRENT。

My solution is to set the flag FLAG_ACTIVITY_NEW_TASK (http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK) in the intent. Then create the Pending intent with id 0 and PendingIntent.FLAG_CANCEL_CURRENT.

Intent _intent = new Intent(context, MyActivity.class);
_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_intent.putExtra("DATA", myData);

PendingIntent _pIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

无论何时通知被点击MyActivity的新任务是创建一个实例,并的onCreate被调用。其目的包含正确的额外数据。 当我preSS后退按钮,有一个新的通知,意图总是带来最新的额外数据。 如果活动的一个实​​例已经存在(例如当我$ PSS主屏幕按钮p $),<一个href="http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)"相对=nofollow> onNewIntent 方法被调用的意图带来正确的新的额外的数据。

Whenever the notification is clicked an instance of MyActivity is created in a new task and onCreate is called. The Intent contains the correct extra data. When I press the back button and there is a new notification, the intent always brings the newest extra data. If an instance of the activity already exists (for example when I press the Home Screen button), onNewIntent method is called and the intent brings the correct new extra data.

这显然是在 http://developer.android解释.COM /引导/主题/清单/活性element.html#LMODE 。 我也尝试过使用每个通知的不同要求codeS(ARG 2)PendingIntent.getActivity和PendingIntent.FLAG_UPDATE_CURRENT,但我认为这个秘密是launchMode作为singleTask和Intent.FLAG_ACTIVITY_NEW_TASK的定义。

This is clearly explained in http://developer.android.com/guide/topics/manifest/activity-element.html#lmode. I also tried using different request codes (arg 2) per notification in PendingIntent.getActivity and with PendingIntent.FLAG_UPDATE_CURRENT but I think the secret is in the definition of launchMode as singleTask and Intent.FLAG_ACTIVITY_NEW_TASK.

我也有另一种情况,其中launchMode是标准。这里的活动也开始通过单击一个服务发送通知。 为了有开始活动得到正确的额外数据我设置标志FLAG_ACTIVITY_SINGLE_TOP。

I also have another case in which the launchMode is "standard". Here the activity is also started by clicking a notification sent from a service. In order to have the started activity receive the correct extra data I set the flag FLAG_ACTIVITY_SINGLE_TOP.

_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

此标志使当活动的一个实​​例已经存在的方法onNewIntent被称为(当我pressed主屏幕按钮例如)。再次与正确的额外数据。否则的onCreate被称为还与正确的额外数据(当我pressed例如后退按钮)。

This flag makes the method onNewIntent to be called when an instance of the activity already exists (When I pressed Home Screen button for example). Again with the correct extra data. Otherwise onCreate is called also with the correct extra data (When I pressed the back button for example).

挂起的意图有一个新的请求code每一次创建,但它应该还与同样要求code。我想,这个秘密是在launchMode和意图标志设置。

The Pending intent is created with a new request code every time but it should work also with the same request code. I think the secret is in the launchMode and the intent flag set.

PendingIntent pIntent = PendingIntent.getActivity(this, notificationRequestCode, _intent, PendingIntent.FLAG_CANCEL_CURRENT);

这篇关于意图用的onCreate()为singleTask活动老多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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