Question: basically my app has 2 activities.Say "A" and "B" . A launches B.

Activity B plays music(in a service) and also has a notification.

when the view is on activity B and i press back button ,and then i click on the the notification, activity B is opened with the state of activity completely lost and music also stops(not desired)......i am making a guess that it happens because when i press the back button the activity is explicitly destroyed ,so i have to progamatically restore its view right??

first thing that comes into mind is using onSavedInstanceState
==============================================================

but that won't help because onSavedInstanceState is not called when user presses back button (i read it in the docs) so what are my alternatives??

Answer: 
To expand on the answers by Hendrik and Alasdair, I suggest looking into using the Decorator plugin.
<http://swiftmailer.org/docs/plugins.html#using-the-decorator-plugin>

Instead of manipulating the individual message parts, the plugin will replace the desired placeholders of the entire message for each recipient.

For example

```
$message = \Swift_Message::newInstance();

$replacements = array();
foreach ($users as $user) {
    $replacements[$user['email']] = array(
        '{username}' => $user['username'],
        '{password}' => $user['password']
    );
    $message->addTo($user['email']);
}
$decorator = new \Swift_Plugins_DecoratorPlugin($replacements);
$mailer->registerPlugin($decorator);

$message
    ->setSubject('Important notice for {username}')
    ->setBody(
        "Hello {username}, we have reset your password to {password}\n" .
        "Please log in and change it at your earliest convenience."
    );
$message->addPart('{username} has been reset with the password: {password}', 'text/plain');
//..
$mailer->send($message);

```

---

Additionally in PHP, objects are passed by reference, so you can directly manipulate the individual parts body, or content type.

```
$textPart = \Swift_MimePart::newInstance('Hello World', 'text/plain');
$htmlPart = clone $textPart;
$htmlPart->setContentType('text/html');
$message->setTo('someone@example.com');
$message->attach($htmlPart);
$message->attach($textPart);
//...
$mailer->send($message);

$textPart->setBody('Something Else');
$htmlPart->setBody('Something Else');
$message->setTo('someone.else@example.com');
$mailer->send($message);

```

You can also remove a child part by using

```
$message->detach($textPart);

```

Instead of using detach, which iterates over the parts, looking at how `addPart` and `attach` work, they simply call `setChildren(array_merge($this->getChildren(), array($part)))`

So you can set the children parts manually by defining them, which replaces calling `addPart` or `attach`.

```
$message->setChildren([$htmlPart, $textPart]);

```

For all intents and purposes, if you're removing parts of a Message along with different content (albeit slightly) for another recipient, you're in effect creating a new Message. The programming logic can reflect that by calling `$message = \Swift_Message::newInstance()` when the message parts need to be replaced.
According to the [Swift Mailer Docs](http://swiftmailer.org/docs/messages.html#setting-the-body-content), there is no such method as a `getBody()` really.

I would suggest you to store the actual message *template* containing your placeholders in a variable and use `setBody()` on every iteration to replace the complete body of the message with the parsed template for the new recipient instead of trying to replace a tiny bit of it.

I reckon your code could look something like this:

```
$template = '<h1>Hello {{firstname}}</h1>, ...';
$recipients = [
    ['firstname' => 'John', 'email' => 'john@example.com'],
    ...
];
$message = Swift_Message::newInstance('Subject here');

foreach ($recipients as $recipient) {
    $body = str_replace('{{firstname}}', $recipient['firstname'], $template);
    $message->setBody($body, 'text/html');

    // continue building the mail object and send it ...
}

```

This way you could indeed re-use the Swift\_Message instance for every mail. Although it wouldn't make any difference to PHP if you re-use the instance or create a new one in every loop iteration.