0

I want to start the "ChatActivity" of my app by click Notification, and when i click back button, we will go to the "MainActivity" of my app. I write my code as the Android Notification Guide. My code is follow.

 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());

My manifest xml is like this:

  <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>

My problem is when I press back button in ChatActivity, the HomeActivity will be recreated even if I already started my app. If I have started my app and stay at the HomeActivity page, I think it should not created again. How to avoid recreate HomeActivity? Any help is grateful.

1 Answers1

0

On ChatActivity class add the following code snippet

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}