簡體   English   中英

當您的應用程序從通知中打開時,是否可以從打開的位置返回到以前的應用程序? 安卓

[英]When your application is opened from notification is it possible to get back to previous application from where it was opened? Android

是否可以從打開應用程序的位置返回到以前的應用程序? 例如,我在應用程序 A 中,然后單擊通知並打開我的應用程序 B,然后在我的應用程序 BI 中單擊一個按鈕,然后返回應用程序 A? 目前我正在嘗試這樣做,而我實現的唯一方法是當我的應用程序在后台運行或僅在第一次運行時。

我正在使用 Flutter,但這些操作是在原生 Android 代碼中執行的,所以我可能遺漏了一些東西。

為了回到以前的應用程序,我正在使用: moveTaskToBack(true);

為了創建通知,我的代碼如下所示:

private void NotifyToAutofill(String uri, String url, NotificationManager notificationManager) {
        if (notificationManager == null || isNullOrWhitespace(uri)) {
            return;
        }

        Context context = getApplicationContext();
        Intent startIntent = new Intent(context, MainActivity.class);
        startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        startIntent.setAction(Intent.ACTION_RUN);

        startIntent.putExtra("uri", uri);
        startIntent.putExtra("url", url.contains("\u2022")
                || url.contains("\u2731") ? "" : url);
        String channelId = "channel-01";
        String channelName = "test";
        int importance = NotificationManager.IMPORTANCE_LOW;


        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(
                    channelId, channelName, importance);
            notificationManager.createNotificationChannel(mChannel);
        }
        int color = 0x008000;
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentTitle("Test Service")
                .setContentText("Tap to open application")
                .setAutoCancel(true);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntent(startIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
        mBuilder.setContentIntent(resultPendingIntent);
        Notification notification = mBuilder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(AutoFillNotificationId, notification);

    }

找到了解決辦法。 我的問題是我正在創建PendingIntent

        stackBuilder.addNextIntent(startIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
        );

當我使用時,一切都按預期工作:

        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);

暫無
暫無

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

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