使用TaskStackBuilder并没有解决我的问题,只适用于 Honeycomb 和更大。所以我采取以下解决方案(请不要把我钉在十字架上):
- 调用
MainActivity
而不是MatchActivity
,作为参数传递MatchActivity
(通过 Intent)。
- 在中,如果参数可用,则
MainActivity.onCreate
启动。MatchActivity
新代码:
Intent resultIntent = new Intent(context, MainActivity.class) //
.putExtra(MainActivity.ACTIVITY_EXTRA, MatchActivity.class.getName()) //
.putExtra("Pass extras to MatchActivity", "if you want! :)");
PendingIntent pendingIntent = PendingIntent.getActivity(context, visitId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context) //
.setContentIntent(pendingIntent) //
.build();
开MainActivity
:
public static final String ACTIVITY_EXTRA = "activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getStringExtra(ACTIVITY_EXTRA) != null) {
startActivity(new Intent(getIntent()).setClassName(this, getIntent().getStringExtra(ACTIVITY_EXTRA)));
}
...
}