在我的应用中,当用户点击应用通知时,我会启动Activity
。
有时,当用户点击通知时,Activity
会正确显示,但有时应用的MainActivity
也会启动正确的Activity
,并在用户点击时显示回到正确的Activity
。
我尝试将"android:launchMode=singleTask"
添加到
AlertActionReceiverActivity
,即从通知中启动的Activit
y,也不添加,但结果相同。
解决这个问题有什么好运吗?
<activity android:name=".MainActivity" android:label="@string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlertActionReceiverActivity" android:launchMode="singleTask" android:excludeFromRecents="true" android:theme="@style/AppThemeTransparent" />
答案 0 :(得分:1)
这是因为在Android上,默认情况下,当您返回时,它是显示的父活动,而不是之前的活动。
解决方案在Android doc上解释:它是使用带有特殊标志的pendingIntent: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse
(请注意API兼容性!!)
我希望这有帮助
答案 1 :(得分:0)
<activity android:name=".MainActivity" android:label="@string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AlertActionReceiverActivity"
android:parentActivityName=".ABCActivity"
android:launchMode="singleTask" android:excludeFromRecents="true" android:theme="@style/AppThemeTransparent">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ABCActivity"/>
</activity>
还要创建正确的TaskStack:
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
stackBuilder.addNextIntent(resultIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent notifyIntent =
new Intent(new ComponentName(this, ResultActivity.class));
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPIntent =
PendingIntent.getActivity(
this,
0,
notifyPIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(notifyPIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());