簡體   English   中英

按下通知時重新創建活動 - 如何?

[英]Recreate Activity when pressing the notification - how to?

我有一個可點擊的通知,點擊后會打開我的應用程序。 然而,舊的活動不會被刪除,所以,當我關閉應用程序時,之前的活動是相同的......希望你理解。

那么,如何刪除舊活動? 下面的代碼位於服務中。 我需要銷毀主要活動。

void sendNotif() {
        Intent in = new Intent(this, MainActivity.class);
        PendingIntent pin = PendingIntent.getActivity(this, 0, in, 0);
        Notification notif = new Notification.Builder(this)
         .setContentTitle("App launched")
         .setContentText("Press to open app")
         .setSmallIcon(R.drawable.ic_launcher)
         .setContentIntent(pin)
         .build();            
        notif.flags |= Notification.FLAG_NO_CLEAR;
        nm.notify(1, notif);
 }

創建這樣的PendingIntent時,為什么不只是在Intent中添加標志:

Intent in = new Intent(this,  getClass()).addFlags(Intent.FLAG_CANCEL_CURRENT));
PendingIntent pin = PendingIntent.getActivity(this, 0, in, 0);

嘗試將這些標志添加到您的意圖中。

in.setFlags(FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK);

參見說明。

科特林:

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)

    val pendingIntent = PendingIntent.getActivity(applicationContext, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT)
onPause()
{
    MainActivity.this.finish();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM