从通知中单击时,使用 backstack 的向上导航不起作用
Up navigation using backstack not working while clicked from notification
我正在从通知中打开一个 activity,可以正常打开。
但是,我想在单击 'back button' 时打开它的父 activity,目前它直接退出应用程序。我希望它导航到 HomeScreenActivity。
这是清单声明 -
<activity
android:name="com.discover.activities.MyTrialsActivity"
android:exported="true"
android:parentActivityName="com.discover.activities.HomeScreenActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.discover.activities.HomeScreenActivity" />
</activity>
这是我生成通知的代码 -
public static PendingIntent getAction(Activity context, int actionId) {
Intent intent;
PendingIntent pendingIntent;
intent = new Intent(context, MyTrialsActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack
//stackBuilder.addParentStack(HomeScreenActivity.class);
stackBuilder.addParentStack(HomeScreenActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(intent);
// Gets a PendingIntent containing the entire back stack
pendingIntent =
stackBuilder.getPendingIntent(0 /*request code */, PendingIntent.FLAG_ONE_SHOT);
/*pendingIntent = PendingIntent.getActivity(context, 0 *//* Request code *//*, intent,
PendingIntent.FLAG_UPDATE_CURRENT*//*|PendingIntent.FLAG_ONE_SHOT*//*);*/
return pendingIntent;
}
/**
* Create and show a simple notification containing the message.
*
* @param message Message to show in notification
*/
public static void sendNotification(Context context, String message, int actionId) {
PendingIntent pendingIntent = NotifUtils.getAction((Activity) context, actionId);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000})
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
我在 android 的文档中找到了解决方案
// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this, DetailsActivity.class);
// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
TaskStackBuilder.create(this)
// add all of DetailsActivity's parents to the stack,
// followed by DetailsActivity itself
.addNextIntentWithParentStack(upIntent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);
而且,
Here 是 link.
此外,请参阅 this 答案以获取更多参考资料。
尝试使用 startActivities(Context context, Intent[] intents)、
Intent homeIntent = new Intent(context, HomeScreenActivity.class);
Intent newIntent = new Intent(context, MyTrialsActivity.class);
Intent[] intents = new Intent[]{homeIntent, newIntent};
ContextCompat.startActivities(context, intents);
因此我们可以同时启动多个 Activity,因此当按下“返回”按钮时它将转到主页而不是退出应用程序。
解-
我在 addParentStack(MyTrialActivity.class);
中也添加了我的 child activity
它按预期工作。
我认为添加 addNextIntent()
应该已经这样做了,尽管它没有那样工作..
我已经正确设置了所有内容,但它仍然无法正常工作,可能是您需要卸载并重新安装该应用程序。当您 运行 应用程序时,清单的某些更改似乎没有正确更新!
我正在从通知中打开一个 activity,可以正常打开。 但是,我想在单击 'back button' 时打开它的父 activity,目前它直接退出应用程序。我希望它导航到 HomeScreenActivity。
这是清单声明 -
<activity
android:name="com.discover.activities.MyTrialsActivity"
android:exported="true"
android:parentActivityName="com.discover.activities.HomeScreenActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.discover.activities.HomeScreenActivity" />
</activity>
这是我生成通知的代码 -
public static PendingIntent getAction(Activity context, int actionId) {
Intent intent;
PendingIntent pendingIntent;
intent = new Intent(context, MyTrialsActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack
//stackBuilder.addParentStack(HomeScreenActivity.class);
stackBuilder.addParentStack(HomeScreenActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(intent);
// Gets a PendingIntent containing the entire back stack
pendingIntent =
stackBuilder.getPendingIntent(0 /*request code */, PendingIntent.FLAG_ONE_SHOT);
/*pendingIntent = PendingIntent.getActivity(context, 0 *//* Request code *//*, intent,
PendingIntent.FLAG_UPDATE_CURRENT*//*|PendingIntent.FLAG_ONE_SHOT*//*);*/
return pendingIntent;
}
/**
* Create and show a simple notification containing the message.
*
* @param message Message to show in notification
*/
public static void sendNotification(Context context, String message, int actionId) {
PendingIntent pendingIntent = NotifUtils.getAction((Activity) context, actionId);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000})
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
我在 android 的文档中找到了解决方案
// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this, DetailsActivity.class);
// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
TaskStackBuilder.create(this)
// add all of DetailsActivity's parents to the stack,
// followed by DetailsActivity itself
.addNextIntentWithParentStack(upIntent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);
而且, Here 是 link.
此外,请参阅 this 答案以获取更多参考资料。
尝试使用 startActivities(Context context, Intent[] intents)、
Intent homeIntent = new Intent(context, HomeScreenActivity.class);
Intent newIntent = new Intent(context, MyTrialsActivity.class);
Intent[] intents = new Intent[]{homeIntent, newIntent};
ContextCompat.startActivities(context, intents);
因此我们可以同时启动多个 Activity,因此当按下“返回”按钮时它将转到主页而不是退出应用程序。
解-
我在 addParentStack(MyTrialActivity.class);
中也添加了我的 child activity
它按预期工作。
我认为添加 addNextIntent()
应该已经这样做了,尽管它没有那样工作..
我已经正确设置了所有内容,但它仍然无法正常工作,可能是您需要卸载并重新安装该应用程序。当您 运行 应用程序时,清单的某些更改似乎没有正确更新!