简体   繁体   English

当您的应用程序从通知中打开时,是否可以从打开的位置返回到以前的应用程序? 安卓

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

Is it possible to return to previous application from where your application is opened?是否可以从打开应用程序的位置返回到以前的应用程序? For example I am in application A then I click on notification and open my application B then in my application BI click a button and I get back to application A?例如,我在应用程序 A 中,然后单击通知并打开我的应用程序 B,然后在我的应用程序 BI 中单击一个按钮,然后返回应用程序 A? Currently I am trying to do that and the only way how I achieved is when my application is running in background or it only works for the first time.目前我正在尝试这样做,而我实现的唯一方法是当我的应用程序在后台运行或仅在第一次运行时。

I am using Flutter but these actions are executed in native Android code so I am probably missing something.我正在使用 Flutter,但这些操作是在原生 Android 代码中执行的,所以我可能遗漏了一些东西。

For getting back to previous app I am using: moveTaskToBack(true);为了回到以前的应用程序,我正在使用: moveTaskToBack(true);

For creating a notification my code looks like this:为了创建通知,我的代码如下所示:

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

    }

Found the solution.找到了解决办法。 My problem was that i was creating PendingIntent with我的问题是我正在创建PendingIntent

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

Everything worked as intented when I used:当我使用时,一切都按预期工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我从菜单中打开“编辑朋友”时,应用程序停止了 - When I opened Edit friends from menu, Application stopped Android:当所有程序发送到后台时,如何关闭从我的应用程序打开的应用程序 - Android: How to close application opened from my app when all sent to background Java-从应用程序刷新打开的HTML页面 - Java - refresh opened html page from application 一种以编程方式知道应用程序何时在Android上处于“打开”状态的方法? - a way to programmatically know when an application is in “opened” state on android? 从通知或外部 Intent 打开应用程序时,SingleTop launchMode 失败 - SingleTop launchMode fails when app is opened from notification or external Intent 如何知道从我的应用程序打开的数据库连接数? - How to know the number of DB connections opened from my application? Html 报告在 Safari 中从 Java 应用程序打开,好像 Z9E13B69D1D2DA927102ACAAAF7 - Html report opened in Safari from Java application displays as if Javascript not enabled 在Android中显示最新打开的应用程序的吐司 - Show toast of the most recently opened application in android BroadcastReceiver仅在打开应用程序时有效 - BroadcastReceiver works only when application is opened 从打开的会话中获取GraphUser? - get GraphUser from an opened session?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM