我有两项活动:MainActivity
和SettingsActivity
。从通知中我启动了SettingsActivity
。当我按回 - >显示MainActivity
,但如果在通知到来时它是活动的,我希望不会重新创建MainActivity
。现在每次重新创建MainActivity
(onCreate()
被调用)
private void sendNotification(HashMap<String, String> data) {
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra("fcm", data);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SettingsActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setColor(getResources().getColor(R.color.colorNotification))
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.notification))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MESSAGE_NOTIFICATION_ID, notificationBuilder.build());
}
清单:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:label="@string/settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>