0

我想通过单击通知启动我的应用程序的“ChatActivity”,当我单击返回按钮时,我们将转到我的应用程序的“MainActivity”。我将我的代码编写为Android Notification Guide。我的代码如下。

 NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setAutoCancel(true)
                    .setCategory(Notification.CATEGORY_MESSAGE)
                    .setContentTitle("你有" + unreadCount + "条新的消息")
                    .setContentText(content)
                    .setNumber((int) unreadCount)
                    //.setColor(Color.RED)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setSmallIcon(R.mipmap.icon_notify_bold);
            TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
            Intent resultIntent = new Intent(context, ChatActivity.class);
            resultIntent.putExtra("to", CommonUtils.removeString(conversation.communicator,
                    ProfileHelper.getProfile().username));
            resultIntent.putExtra("conversationId", conversation.id);
            taskStackBuilder.addParentStack(ChatActivity.class);
            taskStackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(resultPendingIntent);
            manager.notify(NOTIFY_ID_MESSAGE, builder.build());

我的清单 xml 是这样的:

  <activity
        android:name=".ui.HomeActivity"
        android:label="@string/homepage"
        android:launchMode="singleTask"
        android:theme="@style/AppTheme.NoActionBar" />

    <activity
        android:name=".ui.ChatActivity"
        android:label="@string/back"
        android:launchMode="singleTask"
        android:parentActivityName=".ui.HomeActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ui.HomeActivity" />
    </activity>

我的问题是当我在 ChatActivity 中按下返回按钮时,即使我已经启动了我的应用程序,也会重新创建 HomeActivity。如果我已经启动了我的应用程序并停留在 HomeActivity 页面,我认为它不应该再次创建。如何避免重新创建 HomeActivity?任何帮助都将不胜感激。

4

1 回答 1

0

在 ChatActivity 类上添加以下代码片段

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}
于 2016-07-19T10:51:47.287 回答