以恢复历史记录的方式恢复我的应用程序

Ily*_*man 15 android android-activity

我正在编写一个SDK用于托管应用程序.我的SDK会创建一个需要恢复应用的通知,就像按下任务按钮并选择应用一样,或者长按主页按钮并选择您的应用.

这是我一直在尝试做的事情:

        PackageManager packageManager = context.getPackageManager();
        intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
        intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 10, intent, flags);
        Notification notification = new NotificationCompat.Builder(context).
            setContentIntent(pendingIntent).
            ...
            build();

        getNotificationManager().notify(NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)

我一直在主机应用程序上测试这个,其中一个启动器活动具有"默认"的午餐模式(清单中没有设置启动模式),我的sdk还通过午餐模式"singleTask"获得了1个活动.

  1. 所以我在应用程序中午餐
  2. 启动我的SDK活动,它会在onCreat方法中触发测试通知.
  3. 我按回家
  4. 我点击通知.

在执行这些步骤后,我希望返回到我的活动,但它会打开主机启动器活动的另一个实例.我错过了什么?我该如何工作?

小智 0

根据您的要求,主机应用程序需要在启动器活动上设置“ singleTop ”启动模式,并且您无法从您的 SDK 中设置它。您需要以某种方式告诉主机应用程序设置此标志。

设置“singleTop”启动模式后,您将通过重写 onNewIntent() 方法接收新意图,并且您的活动将处于与之前相同的状态。