I am building an Android app which should just open the app once its notifications are clicked.
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
// Gets an instance of the NotificationManager service
mNotificationManager = NotificationManagerCompat.from(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("ALERTS", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
mNotificationManager.createNotificationChannel(channel);
}
}
public void showNotification(String Title, String info, int id){
// Create an Intent for the activity you want to start
Intent resultIntent = new Intent(this, MainActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this, "ALERTS")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(Title)
.setContentText(info)
// Set the intent that will fire when the user taps the notification
.setContentIntent(resultPendingIntent);
mNotificationManager.notify(mNotificationId, builder.build());
}
I followed this guide: https://developer.android.com/training/notify-user/navigation
The notifications display correctly and once I click them it takes me to MainActivity, but it restarts MainActivity. Why does this happen and how can I make it to just open it as if opened from onResume?
[EDIT] added my XML structure
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.example">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:screenOrientation="sensorPortrait"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="..."/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
android:launchMode="singleTop"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Settings"
android:label="Settings"
android:exported="false"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<service
android:name="com.company.example.DeviceService"
android:permission="android.permission.BIND_MIDI_DEVICE_SERVICE"
android:exported="false">
<intent-filter>
<action android:name="android.media.midi.MidiDeviceService" />
</intent-filter>
<meta-data
android:name="android.media.midi.MidiDeviceService"
android:resource="@xml/device_info" />
</service>
</application>
</manifest>