意图没有被singleTop Activity选中

Ste*_*bbi 3 notifications android android-intent

我有一个被定义为singleTop的Activity,因此只存在一个实例.

   <activity
        android:name=".MyMainActivity"
        android:label="@string/app_name" 
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize" 
        android:launchMode="singleTop" android:multiprocess="true">
Run Code Online (Sandbox Code Playgroud)

我使用一些数据设置了Notification Intent,并将其包含在PendingIntent中,并将其发送到Notification Manager.

Intent notificationIntent = new Intent(context, MyMainActivity.class); 
notificationIntent.setAction(MyMainActivity.CustomInternalMessageAction);
notificationIntent.putExtra(MyMainActivity.ReceiveTestMessage, rawMessageText);

...
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
mNotificationManager.notify(number, notification);  
Run Code Online (Sandbox Code Playgroud)

如果actiivty未运行,则intent会按预期通过onCreate()启动活动.

如果活动正在运行,但处于已停止状态/不在前台(如单击主页按钮),并且单击了通知,则会按预期调用我的活动的onNewIntent().

但是,如果活动在前景中已被调用,则不会调用onNewIntent().

我如何设置这个通知,以便当我的singleTop Activity处于暂停/停止状态时发送意图(并且在我提到的其他情况下仍然起作用).我想在我的notificationIntent对象上设置了一个标志,但是对于singleTop活动的情况,标志的功能并不是很清楚.

Lui*_*uis 8

如果您的应用程序由多个活动组成,singleTop那么如果您希望onNewIntent()在该活动处于活动状态时接收呼叫,则还需要定义剩余活动.

假设主要活动是A和被定义为singleTop从那里开始B未定义的活动singleTop.如果现在您选择调用应用程序的通知,则应用程序将从活动开始,BonNewIntent()不会被调用.

您还可以覆盖此行为,添加从堆栈Intent.FLAG_ACTIVITY_CLEAR_TOP中删除活动的标志,B并且Activity A将重新启动调用onNewIntent().