Search code examples
androidandroid-intentandroid-activitynotificationsandroid-pendingintent

android java notification tap reset activity


i use this code for notification

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "notify_001");
 Intent intent = new Intent(getApplicationContext(), MainActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
 intent,PendingIntent.FLAG_UPDATE_CURRENT);
 mBuilder.setContentIntent(pendingIntent); 

when i tap on notification app open and restart MainActivity i dont want to restart MainActivity.


Solution

  • If possible try to add the launch modes to the main activity.

    in manifest under the activity tag just use this tag. Or you can add this mode while starting the pending intent for main activity from the notification manager class.

    android:launchMode="singleTask"
    

    sample code

    <activity
      android:name=".MainActivity"
      android:label="singleTask"
      android:launchMode="singleTask"
      android:taskAffinity="">
    

    this should make the new launch to pass the data to existing activity if any opened and is present on back stack.

    Also make sure you override the below method to receive the new data in main activity

    protected void onNewIntent (Intent intent){
      //your update code goes here
    }