我有一個應用程序,它具有MainActivity
和MessageInboxActivity
。如果用戶有應用程序後臺並收到消息,我會顯示本地通知。當用戶點擊通知時,我想恢復當前正在運行的MainActivity
。但是,在我的測試中,它正在銷燬當前MainActivity
,顯示MessageInboxActivity
,然後點擊後退按鈕將導致創建新的MainActivity
。通過通知打開現有活動
private void SendLocalNotification()
{
var notificationID = 1;
var title = "New Message";
var message = Constants.Message.NewMessageNotificationText;
var builder = new NotificationCompat.Builder(Application.Context);
builder.SetContentTitle(title);
builder.SetContentText(message);
builder.SetAutoCancel(true);
builder.SetSmallIcon(Resource.Drawable.pn_message_icon);
var mainIntent = new Intent(this, typeof(MainActivity));
var finalIntent = new Intent(this, typeof(MessageInboxActivity));
var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(Application.Context);
stackBuilder.AddNextIntent(mainIntent);
stackBuilder.AddNextIntent(finalIntent);
var resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
builder.SetContentIntent(resultPendingIntent);
var notificationManager = NotificationManagerCompat.From(Application.Context);
notificationManager.Notify(notificationID, builder.Build());
}
的MainActivity
設置爲LaunchMode=SingleTask
。
[Activity(Label = "MainActivity", Theme = "@style/MyTheme",
WindowSoftInputMode = SoftInput.AdjustPan, ScreenOrientation = ScreenOrientation.Portrait,
LaunchMode = LaunchMode.SingleTask)]
public class MainActivity : BaseActivity
我該如何讓現有MainActivity
繼續而不是被破壞的新的?我做了一些搜索,並認爲我正確地回答了答案,但似乎沒有任何工作。