如何通过从应用程序通知导航来重新创建 activity?
How to recreate activity by navigating from app notification?
我最近为我的应用程序实现了通知。我一直在测试的是应用程序应该如何工作的行为。我想它会是这样的:
收到通知并通过 intent 打开 Comments.class
-> MainActivity.class
-> 点击返回(退出应用程序,转到 Android 设备的主屏幕) -> 通过任务重新打开应用程序经理 -> 返回 Comments.class
前几个步骤有效,但是当我从任务管理器转到打开备份应用程序时,我注意到它转到 Comments.class
,这似乎是预期的行为,因为我已经用其他 Android 应用。
但是,我的 Comments.class
的一些属性没有被设置,我知道这是因为我的 Comments.class
的 onCreate
方法没有被调用。有什么理由不会发生这种情况吗?这是我的尝试:
NotificationService.java
:
Intent resultIntent = new Intent(context, Comments.class);
Intent backIntent = new Intent(context, MainActivity.class);
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Set a random notification Id for each notification so that if you get multiple, the first does not get replaced.
Random random = new Random();
int notificationId = random.nextInt(9999 - 1000) + 1000;
PendingIntent pendingIntent = PendingIntent.getActivities(context, notificationId, new Intent[] { backIntent, resultIntent}, PendingIntent.FLAG_ONE_SHOT);
//When the notification is actually clicked on
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notificationBuilder.build());
这首先创建通知并通知应用程序的用户。然后 Comments.java
的 onCreate
函数被调用,当我点击 Notification
时这是预期的。然后,我单击返回,它会像这样简单地导航回 MainActivity
:
Comment.java
:
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
然后, MainActivity.java
被调用并创建,这一切都很好。如果我单击 MainActivity.java
,然后执行以下代码:
来自MainActivity.java
@Override
public void onBackPressed() {
super.onBackPressed();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
这会将我带回设备的主屏幕。但是,我的应用程序的一个实例仍在后台 运行,当我在后台单击我的应用程序并将其置于前台时,我被导航回 Comments.java
但是因为 Comments.java
的onCreate
方法没有调用,Activity
的一些细节没有初始化。知道为什么会这样吗?
如有任何帮助,我们将不胜感激。谢谢!
将您的初始化放在 Comment.java
的 onResume
函数中。这是一篇关于 Android Activity Lifecycle 的好书,可以帮助您正确理解行为。
@Override
public void onResume() {
// Your initialization
}
我最近为我的应用程序实现了通知。我一直在测试的是应用程序应该如何工作的行为。我想它会是这样的:
收到通知并通过 intent 打开 Comments.class
-> MainActivity.class
-> 点击返回(退出应用程序,转到 Android 设备的主屏幕) -> 通过任务重新打开应用程序经理 -> 返回 Comments.class
前几个步骤有效,但是当我从任务管理器转到打开备份应用程序时,我注意到它转到 Comments.class
,这似乎是预期的行为,因为我已经用其他 Android 应用。
但是,我的 Comments.class
的一些属性没有被设置,我知道这是因为我的 Comments.class
的 onCreate
方法没有被调用。有什么理由不会发生这种情况吗?这是我的尝试:
NotificationService.java
:
Intent resultIntent = new Intent(context, Comments.class);
Intent backIntent = new Intent(context, MainActivity.class);
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Set a random notification Id for each notification so that if you get multiple, the first does not get replaced.
Random random = new Random();
int notificationId = random.nextInt(9999 - 1000) + 1000;
PendingIntent pendingIntent = PendingIntent.getActivities(context, notificationId, new Intent[] { backIntent, resultIntent}, PendingIntent.FLAG_ONE_SHOT);
//When the notification is actually clicked on
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notificationBuilder.build());
这首先创建通知并通知应用程序的用户。然后 Comments.java
的 onCreate
函数被调用,当我点击 Notification
时这是预期的。然后,我单击返回,它会像这样简单地导航回 MainActivity
:
Comment.java
:
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
然后, MainActivity.java
被调用并创建,这一切都很好。如果我单击 MainActivity.java
,然后执行以下代码:
来自MainActivity.java
@Override
public void onBackPressed() {
super.onBackPressed();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
这会将我带回设备的主屏幕。但是,我的应用程序的一个实例仍在后台 运行,当我在后台单击我的应用程序并将其置于前台时,我被导航回 Comments.java
但是因为 Comments.java
的onCreate
方法没有调用,Activity
的一些细节没有初始化。知道为什么会这样吗?
如有任何帮助,我们将不胜感激。谢谢!
将您的初始化放在 Comment.java
的 onResume
函数中。这是一篇关于 Android Activity Lifecycle 的好书,可以帮助您正确理解行为。
@Override
public void onResume() {
// Your initialization
}