启动挂起的意向,然后返回到已启动该意向的活动



我正在创建一个android应用程序,但我在这个场景中遇到了问题。。我显示一个弹出窗口,这是一个有半透明背景的活动,它有一个允许在弹出窗口之间滑动的视图寻呼机。现在,当我点击这个弹出窗口时,我想启动一个pendingIntent(它不是由我创建的,而是由外部应用程序创建的。例如,由gMail交付的pendingIntnt)。。

一切似乎都正常,但现在问题来了!如果通过点击弹出窗口,我启动了一个外部活动,如gMail,当我退出最后一个活动时,我需要返回到包含其他弹出窗口的前一个活动,而这种情况有时会发生,但并不总是发生!这对我来说至关重要,因为我已经在清单文件中为带有标记android:excludeFromRecents="true"的弹出窗口设置了我的活动,因此,如果我无法返回到此活动,我将无法处理其他挂起的弹出窗口。

显然,android:excludeFromRecents="true"的使用对我来说也是至关重要的

我怎样才能找到这个问题的解决方案?

以下是我如何从弹出窗口启动pendingIntent的示例:

setOnClickListener(new View.OnViewClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            //intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            if(blablabla){
                try{
                    pi.send(context,0,intent);
                }
                catch(Exception e){}
            }
        }
    });

谢谢大家!!!

编辑

我的清单:

<application
    android:allowBackup="true"
    android:icon="..."
    android:label="..."
    android:theme="..." >
    <activity
        android:name="...ActivityApp"
        android:label="..."
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name="...NotificationListener"
        android:label="..."
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>
    <!-- the activity below contains all the popups   -->
    <activity
        android:name="...Popup"
        android:excludeFromRecents="true"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Translucent">
    </activity>

</application>

最后是我的监听器内部的一段代码,它启动了包含弹出窗口的活动:

@Override
public void onNotificationPosted(StatusBarNotification sbn){
    if(sbn.getPackageName().equalsIgnoreCase("com.google.android.gm")){  // GMAIL
            Log.d("GMAIL","ok");
            ...
            Intent gmailIntent = new Intent(getBaseContext(), Popup.class);
            gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ...
            getBaseContext().startActivity(gmailIntent);
        }

您正在将launchMode="singleInstance"用于Popup活动。这意味着只会创建此活动的一个实例。当有2个通知时,您将调用startActivity() 2次,但第二次它不会创建Popup的另一个实例,它只会将Popup的现有实例带到前台,并在该实例上调用onNewIntent()

此外,您需要在Popup的清单定义中指定taskAffinity="",因为目前Popup活动与主活动具有相同的taskAffinity,如果主活动已经在运行,这可能会导致问题。如果您确实需要singleInstance活动,则必须确保它不会与应用程序中的任何其他活动共享taskAffinity

由于PopupsingleInstance活动,当它启动Gmail活动时,该活动将在新任务中启动(因为singleInstance阻止在由singleInstance活动启动的任务中启动任何其他活动)。这可能是从Gmail按BACK无法返回到您的应用程序的部分原因。

正如我之前所说,恐怕您的体系结构有缺陷,您应该重新考虑您的需求。

最新更新