【发布时间】:2012-10-30 08:56:52
【问题描述】:
我正在实施 GCM。我的应用程序有两个活动,例如 A 和 B。我正在使用此代码从 NotificationBar 启动 B:
long when = System.currentTimeMillis();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = context.getString(R.string.app_name);
Notification notification = new Notification(R.drawable.app_notification_icon, "De Centrale", when);//message
Intent notificationIntent = new Intent(context, B.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); //|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
NotificationBar 使用 Intent 打开 Activity B,例如“B-notification-intent”,然后我使用 Back 按钮从 B 打开 Activity A,然后我再次从 A 启动 B新意图(比如“BA-intent”)。我使用下面的代码:
intent = new Intent(this, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
然后我在B 中获得新数据(即刷新B 的屏幕)。
但是,如果我按下主页按钮,然后我从最近的应用程序启动应用程序,那么我会得到较旧的 B 屏幕,并带有“B-notification-intent”。相反,我想要最新的 Intent,即“B-A-intent”。我在B 中使用此代码:
@Override
protected void onCreate(Bundle b)
{
fetchDataFromDB(getIntent());
}
@Override
protected void onNewIntent(Intent intent)
{
fetchDataFromDB(intent);
}
所以请任何人帮助我获取我当前的屏幕(意图)。
【问题讨论】:
-
尝试将其更改为 on resume protected void onResume(Bundle b) { fetchDataFromDB(getIntent()); }
-
@droidhot 谢谢,你解决了我的问题
-
@droidhot 请解释你的答案(我做错了什么)并将其发布在下面的答案块中
-
当你按下主页按钮时,Activity 进入暂停状态,当它返回时,会调用 resume,并且不会调用 start,因此数据将是 on 时的意图创建在您的情况下是通知意图引用活动生命周期以更好地理解它
-
点击通知后不刷新??无论如何,这不是恢复的问题(请检查活动生命周期),您可以创建一个函数或接收器来解决问题——(我希望如果你点击它显示刷新的屏幕的通知)——如果你注意到facebook 应用程序它不会用新的推送刷新当前屏幕,但你必须拉刷新--
标签: android android-intent notifications flags